标签:
/** * Upload comoms-fileupload */ DiskFileItemFactory diskFileItemFactory = new DiskFileItemFactory(); ServletFileUpload sfu = new ServletFileUpload(diskFileItemFactory); List<FileItem> fileItemList = null; try { fileItemList = sfu.parseRequest(request); } catch (FileUploadException e) { e.printStackTrace(); } FileItem fi0 = fileItemList.get(0); FileItem fi1 = fileItemList.get(1); response.getWriter().println(fi0.getFieldName() + ":" + fi0.getString()); response.getWriter().println(fi1.getName() + ":" + fi1.getSize()); try { fi1.write(new File("c:/haha.jpg")); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } /** * Download * 两个头一个流 */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String filename = "D:/Download/the first time.rmvb"; String ext = filename.substring(filename.lastIndexOf(".")); String disposition = "attachment;filename="+UUID.randomUUID()+ext; String mineType = this.getServletContext().getMimeType(filename); FileInputStream fis = new FileInputStream(filename); response.setHeader("Content-Type", mineType); response.setHeader("Content-Disposition", disposition); ServletOutputStream output = response.getOutputStream(); IOUtils.copy(fis, output); fis.close(); }
/** * download中文文件名字乱码问题 */ package cn.itcast.down.utils; import java.io.IOException; import java.io.UnsupportedEncodingException; import java.net.URLEncoder; import javax.servlet.http.HttpServletRequest; import sun.misc.BASE64Encoder; public class DownUtils { public static String filenameEncoding(String filename, HttpServletRequest request) throws IOException { String agent = request.getHeader("User-Agent"); //获取浏览器 if (agent.contains("Firefox")) { BASE64Encoder base64Encoder = new BASE64Encoder(); filename = "=?utf-8?B?" + base64Encoder.encode(filename.getBytes("utf-8")) + "?="; } else if(agent.contains("MSIE")) { filename = URLEncoder.encode(filename, "utf-8"); } else { filename = URLEncoder.encode(filename, "utf-8"); } return filename; } }
标签:
原文地址:http://www.cnblogs.com/luowen/p/4345705.html