简体   繁体   中英

Empty csv file downloading in jersey rest api

I am trying to download the CSV file using rest api. I am using opencsv library to convert Bean to csv.

I have done debugging all the inputs like inputData and fileName are coming proper and StatefulBeanToCsv is writing to outputstream still empty file is downloading. Please find the code below

Resource method

@GET
@Path("/{category}")
@Produces(MediaType.APPLICATION_OCTET_STREAM)
public Response downloadAdminFile(@PathParam("category") String adminFileCategory,@QueryParam ("subCategory") String subCategory) {
        AdminFileStreamingOutput streamingOutput = adminFileDataService.downloadAdminFile(adminFileCategory,subCategory);
        return Response.ok(streamingOutput).header("Content-Disposition", "attachment; filename=" + streamingOutput.getFileName() + ";" + "charset=UTF-8")
                .build();
}

AdminFileStreamingOutput.java

public class AdminFileStreamingOutput implements StreamingOutput {

    private Collection<?extends Serializable> inputData;

    private String fileName;

    public AdminFileStreamingOutput(Collection<?extends Serializable> inputData,String fileName) {
        this.inputData = inputData;
        this.fileName = fileName;
    }

    @Override
    public void write(OutputStream output) throws IOException, WebApplicationException {
        StatefulBeanToCsvBuilder builder = new StatefulBeanToCsvBuilder(new OutputStreamWriter(output));
        StatefulBeanToCsv beanWriter = builder
                .withSeparator(';')
                .build();
        try {
            beanWriter.write(this.inputData.iterator());
        } catch (CsvDataTypeMismatchException | CsvRequiredFieldEmptyException  e) {
            throw new RuntimeException("Failed to download admin file");
        }
    }
    public String getFileName() {
        return fileName;
    }
}

One of the Bean which is using OpenCSV annotations for mapping strategy

public class ProjectInfo implements Serializable {

    @CsvBindByName(column = "ProjectName",required = true)
    private String projectName;

    @CsvBindByName(column = "ProjectCode",required = true)
    private String projectCode;

   //setters and getters
}

Can anyone help me to find out the issue?. Thanks in advance.

After using flush it's downloading the file with content. Here is the changes

@Override
    public void write(OutputStream output) throws IOException, WebApplicationException {
        try(BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(output))){
            StatefulBeanToCsvBuilder builder = new StatefulBeanToCsvBuilder(writer);
            StatefulBeanToCsv beanWriter = builder
                    .withSeparator(';').build();
            try {
                beanWriter.write(this.inputData.iterator());
                writer.flush();
            } catch (CsvDataTypeMismatchException | CsvRequiredFieldEmptyException  e) {
                throw new CMMV1Exception("Failed to download admin file");
            }
        }

    }

@ Paul Samsotha thanks a lot for your comment

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM