java中客户端拿到下载文件返回的文件流,如何生成文件呢?

客户端得到文件流,但是将其写出到文件的时候,生成的文件总是打不开,这个是怎么回事啊?求助!!!!!

如果说生成的文件打不开的话,我想你上传文件的时候是否有把文件的后缀上传上去,或者说你下载的时候获取文件名有没有把后缀漏掉

我觉得是写出的流出了问题,流的格式什么的

你把这个response.setContentType("application/x-msdownload;charset=UTF-8");换成response.setContentType("multipart/form-data"); 试一下

你的流你要清楚是什么格式的,是TXT、JPG还是PDF还是Excel的;不同的流你最后保存的后缀名是不同的,请使用commons-io的方法达到目的,参考下列代码:


String filePath = "C:/123.txt";
InputStream in = null;
FileUtils.copyInputStreamToFile(in,new File(filePath));

你debug看下你从请求端返回的流的字节有多长

InputStream is = null; OutputStream os = null; is = new FileInputStream(tempFileRootPath+"/"+file_path+file_name);String temp = tempFileRootPath+"/"+file_path+file_name; response.setHeader("Content-disposition", "attachment;filename="+ new String(temp.getBytes("GBK"),"ISO-8859-1")); response.setContentType("application/x-msdownload;charset=UTF-8"); os = response.getOutputStream(); int i = 0;byte[] b = new byte[10240]; while ((i = is.read(b, 0, b.length)) != -1) { os.write(b, 0, i); } os.flush();is.close(); os.close();源码的格式是这样的。