java上传文件跟批量下载文件
最近的项目中涉及到文件的上传跟下载的问题,就自己所涉及到的方面做出如下表述。
首先是文件上传部分,项目的要求是通用性较好,所以只需要传入目标路径即可。参数的传递通过Form表单传值,在目标路径下新建一个File类型的文件,然后通过流的方式将需要上传的文件写入新建的文件中。此方法适用于web开发过程中上传文档类的文件,如果你文件过大请研究ftp相关的知识,笔者所接触的ftp传文件限于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 entry109 bInStream.close(); //关闭110 inStream.close();111 }112 }113 else114 {115 throw new ServletException("文件不存在!");116 }117 }118 catch(IOException e)119 {120 throw e;121 }122 }123 /**124 * 下载打包的文件125 * @param file126 * @param response127 */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 // 清空response136 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就能实现,可以从上面的批量下载中提取单个文件下载的方法。
涉及到文件名称编码的问题,这里提供一个格式化中文字符串的方法。
?
Integer.toHexString(k).toUpperCase()); 22 } 23 } 24 } 25 return sb.toString(); 26 }?
第一次写,欢迎各位大牛指点。如有不正之处欢迎指出。希望对有需要的园友有帮助,笔者将不甚欢喜!