简体   繁体   English

在球衣休息api下载空的csv文件

[英]Empty csv file downloading in jersey rest api

I am trying to download the CSV file using rest api. 我正在尝试使用rest api下载CSV文件。 I am using opencsv library to convert Bean to csv. 我使用opencsv库将Bean转换为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. 我已经完成调试所有输入,如inputData和fileName正在进行, StatefulBeanToCsv正在写入outputstream仍然是空文件正在下载。 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 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 其中一个使用OpenCSV注释进行映射策略的Bean

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. 使用flush后,它会下载包含内容的文件。 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 @ Paul Samsotha非常感谢您的评论

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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