gpt4 book ai didi

java - 下载 .csv 文件 Java6

转载 作者:行者123 更新时间:2023-11-29 04:09:19 25 4
gpt4 key购买 nike

我正在尝试从服务器下载文件。

我有这个方法,但是当我打开下载的文件时,它是字节码,而且不可读。

            File file = new File(request.getPath());
response.setContentType("text/csv;charset=utf-8");
response.setHeader("Content-Disposition","attachment; filename="+file.getName());
response.setHeader("filename",file.getName());
OutputStream resOs= response.getOutputStream();
OutputStream buffOs= new BufferedOutputStream(resOs);
OutputStreamWriter outputwriter = new OutputStreamWriter(buffOs);

FileInputStream inputStream = new FileInputStream(file); //read the file
try {
int c;
while ((c = inputStream.read()) != -1) {
outputwriter.write(c);
}
} finally {
inputStream.close();
outputwriter.flush();
outputwriter.close();
}
};

问题出在哪里?

最佳答案

由于您的方法写入字节数据,因此将内容类型设置为 application/octet-stream,

            response.setContentType("application/octet-stream");

关于java - 下载 .csv 文件 Java6,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56041384/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com