从字符串变量创建包含文本/csv 的 zip 文件的下载
Posted
技术标签:
【中文标题】从字符串变量创建包含文本/csv 的 zip 文件的下载【英文标题】:Creating a download for zip file containing text/csv from string variables 【发布时间】:2017-11-22 05:32:36 【问题描述】:我目前需要创建一个 zip 文件以供下载。这应该包含两 (2) 个要从字符串变量创建的 csv 文件。我不知道该怎么做。我的草稿在下面。
public @ResponseBody Object getFileV1(HttpServletRequest request, HttpServletResponse response)
try
response.setContentType("application/zip");
response.setHeader("Content-Disposition", "attachment; filename=Reassigned Tickets Report " + new Date().toString() + ".zip");
String stringValue1 = "This is a test value for csv1";
String stringValue2 = "This is a test value for csv2";
InputStream is1 = new ByteArrayInputStream(stringValue1.getBytes("UTF-8"));
InputStream is2 = new ByteArrayInputStream(stringValue2.getBytes("UTF-8"));
ZipInputStream zin;
ZipEntry entry;
ZipOutputStream zout= new ZipOutputStream(response.getOutputStream());
zin = new ZipInputStream(is1);
entry = zin.getNextEntry();
zout.putNextEntry(entry);
zin = new ZipInputStream(is2);
entry = zin.getNextEntry();
zout.putNextEntry(entry);
zout.closeEntry();
zin.close();
zout.close();
response.flushBuffer();
return null;
catch (Exception e)
e.printStackTrace();
return e;
显然这是行不通的。可能是因为我还是个新手。请多多包涵。
我在调用“zout.putNextEntry”的那一行得到一个“java.lang.NullPointerException”。非常感谢您的建议。提前谢谢你。
【问题讨论】:
【参考方案1】:经过一天的环顾,我解决了我的问题。这对我有用。但我不确定这是否是最有效的方法。
public @ResponseBody Object getFileV1(HttpServletRequest request, HttpServletResponse response)
try
response.setContentType("application/zip");
response.setHeader("Content-Disposition", "attachment; filename=Test Report " + new Date().toString() + ".zip");
String stringValue1 = "This is a test value for csv1";
String stringValue2 = "This is a test value for csv2";
PrintWriter writer1 = new PrintWriter(new OutputStreamWriter(new FileOutputStream("stringValue1.csv"), "UTF-8"));
writer1.print(stringValue1);
writer1.close();
PrintWriter writer2 = new PrintWriter(new OutputStreamWriter(new FileOutputStream("stringValue2.csv"), "UTF-8"));
writer2.print(stringValue2);
writer2.close();
File file1 = new File("stringValue1.csv");
File file2 = new File("stringValue2.csv");
filesToZip(response, file1, file2);
file1.delete();
file2.delete();
response.flushBuffer();
return null;
catch (Exception e)
e.printStackTrace();
return e;
这是我从另一个线程得到的方法,经过一些修改。
public static void filesToZip(HttpServletResponse response, File... files) throws IOException
// Create a buffer for reading the files
byte[] buf = new byte[1024];
// create the ZIP file
ZipOutputStream out = new ZipOutputStream(response.getOutputStream());
// compress the files
for(int i=0; i<files.length; i++)
FileInputStream in = new FileInputStream(files[i].getName());
// add ZIP entry to output stream
out.putNextEntry(new ZipEntry(files[i].getName()));
// transfer bytes from the file to the ZIP file
int len;
while((len = in.read(buf)) > 0)
out.write(buf, 0, len);
// complete the entry
out.closeEntry();
in.close();
// complete the ZIP file
out.close();
我唯一不喜欢的是我必须创建临时文件并在处理后删除它们。
【讨论】:
以上是关于从字符串变量创建包含文本/csv 的 zip 文件的下载的主要内容,如果未能解决你的问题,请参考以下文章
如何请求一个 zip 文件,提取它,然后从 csv 文件创建熊猫数据框?