最近的项目中涉及到文件的上传跟下载的问题,就自己所涉及到的方面做出如下表述。
首先是文件上传部分,项目的要求是通用性较好,所以只需要传入目标路径即可。参数的传递通过Form表单传值,在目标路径下新建一个File类型的文件,然后通过流的方式将需要上传的文件写入新建的文件中。此方法适用于web开发过程中上传文档类的文件,如果你文件过大请研究ftp相关的知识,笔者所接触的ftp传文件限于C#中,这里不做表述。具体代码如下:
1 public void fileUpload(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 2 { 3 String filePath = new String(); 4 request.setCharacterEncoding("UTF-8"); 5 response.setContentType("text/html; charset=GB2312"); 6 try 7 { 8 DiskFileItemFactory factory = new DiskFileItemFactory(); 9 //设置缓存中最大允许缓存为2M 10 factory.setSizeThreshold(2 * 1024 * 1024); 11 ServletFileUpload upload = new ServletFileUpload(factory); 12 //解决中文文件名为乱码的问题 13 upload.setHeaderEncoding("UTF-8"); 14 List<FileItem> fileList = upload.parseRequest(request); 15 Iterator iter = fileList.iterator(); 16 String newFileName = ""; 17 while (iter.hasNext()) 18 { 19 //获取文件 20 FileItem fileItem = (FileItem)iter.next(); 21 //获取文件上传的路径 22 String typeName = fileItem.getFieldName(); 23 if(("destPath").equals(typeName)) 24 { 25 filePath = fileItem.getString("utf-8"); 26 } 27 if(("filename").equals(typeName)) 28 { 29 newFileName = fileItem.getString("utf-8"); 30 } 31 String fileName = new String(); 32 if (!fileItem.isFormField()) 33 { 34 String name = fileItem.getName(); 35 if(StringUtil.isNullOrEmpty(name)) 36 { 37 continue; 38 } 39 fileName = name; 40 fileName = fileName.substring(fileName.lastIndexOf("\\") + 1); 41 File file = new File(filePath); 42 if(!file.exists()) 43 { 44 file.mkdirs(); 45 } 46 //向指定的路径写文件 47 if(newFileName.length()>0) 48 { 49 fileName = newFileName; 50 } 51 fileItem.write(new File(filePath, fileName)); 52 } 53 } 54 } 55 catch (Exception ex) 56 { 57 throw new ServletException("上传文件失败!", ex); 58 } 59 }
第二部分是关于文件批量下载的文档。根据客户的观点有上传就要下载的需求,一开始也做了相关的开发,结果发现下载时的文件全部存在服务器端,跟需求不一致。仔细推敲了下代码原来自己写的有问题,在客户端是选择文件的存储路径,然后将文件下载,其实只是实现了本地的迁移,并没有在把文件下载到客户端。后来的想法是把文件打包下载,用FileOutputStream,实现下载的问题,可是如果下载的文件中含有中文字符,就会出现乱码。不得已百度了下,原来是编码的问题,使用java自带的文件输出类不能解决压缩文件中文件名乱码的问题。解决方法:使用ant.jar包,创建压缩文件时,可以设置文件的编码格式,文件名乱码的问题就解决了。由于笔者在开发过程中数据库中有文件的路径,所以下列文件的文件路径的获取是查询得到。
具体的解决代码如下:
引用ant.jar包中的FileOutputStream类
import org.apache.tools.zip.ZipEntry; import org.apache.tools.zip.ZipOutputStream;
打包下载多个文件的思路就是在服务端创建一个新的压缩文件(zip格式),然后将下载的多个文件写入该压缩包中,然后以流的形式输出压缩文件写到客户端,实现下载功能。
1 public void downloadFiles(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 2 { 3 request.setCharacterEncoding("UTF-8"); 4 String docId = new String(request.getParameter("docId").getBytes("ISO-8859-1"), "UTF-8"); 5 String downloadType = new String(request.getParameter("downloadType").getBytes("ISO-8859-1"), "UTF-8"); 6 7 Document doc = null; 8 try 9 { 10 doc = getFilePath(docId); //此方法获取文件的路径,这个不显示 11 } 12 catch (SQLException ex) 13 { 14 ex.printStackTrace(); 15 } 16 17 List<Element> elements = doc.getRootElement().elements(); 18 List<File> files = new ArrayList<File>(); 19 int index = 0; 20 long fileLength = 0; 21 for(Element ele : elements) 22 { 23 String filePath = FormatUtil.tryGetItemText(ele, "FILE_PATH", "") + FormatUtil.tryGetItemText(ele, "FILE_NAME", ""); 24 File file = new File(filePath); 25 files.add(file); 26 fileLength += file.length(); 27 index++; 28 } 29 String fileName = UUID.randomUUID().toString() + ".zip"; 30 //在服务器端创建打包下载的临时文件 31 String outFilePath = "C:\\" + fileName; 32 File file = new File(outFilePath); 33 //文件输出流 34 FileOutputStream outStream = new FileOutputStream(file); 35 //压缩流 36 ZipOutputStream toClient = new ZipOutputStream(outStream); 37 toClient.setEncoding("gbk"); 38 zipFile(files, toClient); 39 toClient.close(); 40 outStream.close(); 41 this.downloadZip(file, response); 42 } 43 /** 44 * 压缩文件列表中的文件 45 * @param files 46 * @param outputStream 47 * @throws IOException 48 */ 49 public static void zipFile(List files, ZipOutputStream outputStream) throws IOException,ServletException 50 { 51 try 52 { 53 int size = files.size(); 54 //压缩列表中的文件 55 for(int i = 0; i < size; i++) 56 { 57 File file = (File) files.get(i); 58 zipFile(file, outputStream); 59 } 60 } 61 catch(IOException e) 62 { 63 throw e; 64 } 65 } 66 /** 67 * 将文件写入到zip文件中 68 * @param inputFile 69 * @param outputstream 70 * @throws Exception 71 */ 72 public static void zipFile(File inputFile, ZipOutputStream outputstream) throws IOException,ServletException 73 { 74 try{ 75 if(inputFile.exists()) 76 { 77 if(inputFile.isFile()) 78 { 79 FileInputStream inStream = new FileInputStream(inputFile); 80 BufferedInputStream bInStream = new BufferedInputStream(inStream); 81 ZipEntry entry = new ZipEntry(inputFile.getName()); 82 outputstream.putNextEntry(entry); 83 84 final int MAX_BYTE = 10 * 1024 *1024; //最大的流为10M 85 long streamTotal = 0; //接受流的容量 86 int streamNum = 0; //流需要分开的数量 87 int leaveByte = 0; //文件剩下的字符数 88 byte[] inOutbyte; //byte数组接受文件的数据 89 90 streamTotal = bInStream.available(); //通过available方法取得流的最大字符数 91 streamNum = (int)Math.floor(streamTotal / MAX_BYTE); //取得流文件需要分开的数量 92 leaveByte = (int)streamTotal % MAX_BYTE; //分开文件之后,剩余的数量 93 94 if (streamNum > 0) 95 { 96 for(int j = 0; j < streamNum; ++j) 97 { 98 inOutbyte = new byte[MAX_BYTE]; 99 //读入流,保存在byte数组 100 bInStream.read(inOutbyte, 0, MAX_BYTE); 101 outputstream.write(inOutbyte, 0, MAX_BYTE); //写出流 102 } 103 } 104 //写出剩下的流数据 105 inOutbyte = new byte[leaveByte]; 106 bInStream.read(inOutbyte, 0, leaveByte); 107 outputstream.write(inOutbyte); 108 outputstream.closeEntry(); //Closes the current ZIP entry and positions the stream for writing the next entry 109 bInStream.close(); //关闭 110 inStream.close(); 111 } 112 } 113 else 114 { 115 throw new ServletException("文件不存在!"); 116 } 117 } 118 catch(IOException e) 119 { 120 throw e; 121 } 122 } 123 /** 124 * 下载打包的文件 125 * @param file 126 * @param response 127 */ 128 public void downloadZip(File file,HttpServletResponse response) { 129 try { 130 // 以流的形式下载文件。 131 BufferedInputStream fis = new BufferedInputStream(new FileInputStream(file.getPath())); 132 byte[] buffer = new byte[fis.available()]; 133 fis.read(buffer); 134 fis.close(); 135 // 清空response 136 response.reset(); 137 138 OutputStream toClient = new BufferedOutputStream(response.getOutputStream()); 139 response.setContentType("application/octet-stream"); 140 response.setHeader("Content-Disposition", "attachment;filename=" + file.getName()); 141 toClient.write(buffer); 142 toClient.flush(); 143 toClient.close(); 144 file.delete(); //将生成的服务器端文件删除 145 } 146 catch (IOException ex) { 147 ex.printStackTrace(); 148 } 149 }
单个文件的下载直接下载文件即可,使用java自带的FileOutputStream就能实现,可以从上面的批量下载中提取单个文件下载的方法。
涉及到文件名称编码的问题,这里提供一个格式化中文字符串的方法。
1 public static String toUtf8String(String s){ 2 StringBuffer sb = new StringBuffer(); 3 for (int i = 0;i < s.length(); i++){ 4 char c = s.charAt(i); 5 if (c >= 0 && c <= 255) 6 { 7 sb.append(c); 8 } 9 else{ 10 byte[] b; 11 try 12 { 13 b = Character.toString(c).getBytes("utf-8"); 14 } 15 catch (Exception ex) { 16 b = new byte[0]; 17 } 18 for (int j = 0; j < b.length; j++) { 19 int k = b[j]; 20 if (k < 0) k += 256; 21 sb.append("%" + Integer.toHexString(k).toUpperCase()); 22 } 23 } 24 } 25 return sb.toString(); 26 }
第一次写,欢迎各位大牛指点。如有不正之处欢迎指出。希望对有需要的园友有帮助,笔者将不甚欢喜!