文件上传和下载-创新互联
一、文件上传(commons-fileupload-1.2.1.jar和commons-io-2.0.jar)
创新互联公司-专业网站定制、快速模板网站建设、高性价比江阴网站开发、企业建站全套包干低至880元,成熟完善的模板库,直接使用。一站式江阴网站制作公司更省心,省钱,快速模板网站建设找我们,业务覆盖江阴地区。费用合理售后完善,十载实体公司更值得信赖。public void service(ServletRequest request, ServletResponse response) throws ServletException, IOException { HttpServletRequest httpServletRequest = (HttpServletRequest) request; DiskFileItemFactory fileItemFactory = new DiskFileItemFactory(); fileItemFactory.setSizeThreshold(1024*10); fileItemFactory.setRepository(new File("d:\\tmp")); ServletFileUpload servletFileUpload = new ServletFileUpload(fileItemFactory); servletFileUpload.setSizeMax(1024*1000); try { Listlists = servletFileUpload.parseRequest(httpServletRequest); if(lists!=null){ for(FileItem fileItem : lists){ if(fileItem.isFormField()){ String fieldName = fileItem.getFieldName(); String fieldValue = fileItem.getString(); }else{ InputStream inputStream = fileItem.getInputStream(); String fileName = fileItem.getName(); byte[] b = new byte[1024]; int len=0; FileOutputStream fileOutputStream = new FileOutputStream("d:\\"+fileName); while((len=inputStream.read(b))!=-1){ fileOutputStream.write(b, 0, len); } fileOutputStream.close(); inputStream.close(); } } } } catch (FileUploadException e) { e.printStackTrace(); } }
文件下载
public void service(ServletRequest request, ServletResponse response) throws ServletException, IOException { HttpServletResponse httpServletResponse = (HttpServletResponse) response; httpServletResponse.setContentType("application/x-msdownload"); String fileName = "爱情码头郑源.mp3"; httpServletResponse.setHeader("Content-Disposition", "p_w_upload;filename=" + URLEncoder.encode(fileName, "UTF-8")); OutputStream outputStream = response.getOutputStream(); InputStream inputStream = new FileInputStream("F:\\音乐\\" + fileName); byte[] b = new byte[1024]; int len = 0; while((len=inputStream.read(b))!=-1){ outputStream.write(b, 0, len); } inputStream.close(); }附件:http://down.51cto.com/data/2366514
另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。
本文标题:文件上传和下载-创新互联
链接地址:http://ybzwz.com/article/coeddg.html