我想在java中实现大文件的上传(大概500M),
自己写的上传小一点文件还行,大一点的就崩溃了!!!
有没有这方面的例子????
不胜感激!!!!!!!!!!!
------解决方案--------------------------------------------------------
设置好缓冲500M应该是能承受的喵~~``
不知道楼主怎么实现的...
------解决方案--------------------------------------------------------
500M不是个小数目,能上传也很慢把
------解决方案--------------------------------------------------------
这么大的文件,晕哦,客户端提交数据,肯定全部都会缓存到你的服务器上来,你服务器内存不够肯定死了撒,呵呵,这么大的文件用其他方式吧,边读边写。用web上传它是一并传上来的,你搞不定的。
------解决方案--------------------------------------------------------
用apache上传组件吧
------解决方案--------------------------------------------------------
我这有个例子,没写注释
- Java code
package com.fuyou;import java.io.File;import java.io.IOException;import java.io.PrintWriter;import java.util.Iterator;import java.util.List;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import org.apache.commons.fileupload.FileItem;import org.apache.commons.fileupload.FileItemFactory;import org.apache.commons.fileupload.FileUploadException;import org.apache.commons.fileupload.disk.DiskFileItemFactory;import org.apache.commons.fileupload.servlet.ServletFileUpload;public class Fileupload extends HttpServlet { /** * Constructor of the object. */ public Fileupload() { super(); } /** * Destruction of the servlet. <br> */ public void destroy() { super.destroy(); // Just puts "destroy" string in log // Put your code here } /** * The doGet method of the servlet. <br> * * This method is called when a form has its tag value method equals to get. * * @param request * the request send by the client to the server * @param response * the response send by the server to the client * @throws ServletException * if an error occurred * @throws IOException * if an error occurred */ public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); PrintWriter out = response.getWriter(); out .println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">"); out.println("<HTML>"); out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>"); out.println(" <BODY>"); out.print(" This is "); out.print(this.getClass()); out.println(", using the GET method"); out.println(" </BODY>"); out.println("</HTML>"); out.flush(); out.close(); } /** * The doPost method of the servlet. <br> * * This method is called when a form has its tag value method equals to * post. * * @param request * the request send by the client to the server * @param response * the response send by the server to the client * @throws ServletException * if an error occurred * @throws IOException * if an error occurred */ public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { System.out.println("requestCo"+"=="+request.getContentType()); response.setContentType("text/html;charset=gb2312"); PrintWriter out = response.getWriter(); // 设置保存的文件的目录 String uploadDir = getServletContext().getRealPath("/upload"); System.out.println(uploadDir); if (uploadDir == null) { out.println("无法访问存储目录!"); return; } File fUploadDir = new File(uploadDir); if (!fUploadDir.exists()) { if(!fUploadDir.mkdir()){ out.println("无法创建存储目录!"); return; } } if (!ServletFileUpload.isMultipartContent(request)) { out.println("只能外理mul的类型的数据"); return; } FileItemFactory factory = new DiskFileItemFactory(); ServletFileUpload fu = new ServletFileUpload(factory);//关键类 fu.setSizeMax(1024 * 1024 * 400); // FileItemFactory factory = new DiskFileItemFactory(); DiskFileItemFactory s = new DiskFileItemFactory();//设置临时文件最大值,如果大于这个值,就建一临时文件 s.setSizeThreshold(1024*1024); fu.setHeaderEncoding("gb2312"); List fileItem = null; try { fileItem = fu.parseRequest(request); } catch (FileUploadException e) { out.println("解析出错了"); e.printStackTrace(out); return; } Iterator i = fileItem.iterator(); while (i.hasNext()) { FileItem fi = (FileItem) i.next(); if (fi.isFormField()) { String content = fi.getString("gb2312"); String fieldName = fi.getFieldName(); System.out.println(fieldName+content); } else { String pathSrc = fi.getName(); if (pathSrc.trim().equals("")) { continue; } int start = pathSrc.lastIndexOf("\\"); String fileName = pathSrc.substring(start + 1); File pathDest = new File(uploadDir, fileName); try { fi.write(pathDest); String fieldName = fi.getFieldName(); request.setAttribute(fileName, fileName); } catch (Exception e) { out.println("存储文件出现问题"); e.printStackTrace(out); return; }finally{ fi.delete(); } } } } /** * Initialization of the servlet. <br> * * @throws ServletException * if an error occurs */ public void init() throws ServletException { // Put your code here }}