如题 在网上找的资料良莠不齐 谢谢 |
|
用,uploadfileify
|
|
还真是直接!
|
|
40分 |
package com; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.io.PrintWriter; import java.util.ArrayList; import java.util.Iterator; import java.util.List; import javax.servlet.ServletConfig; 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.FileUploadBase.SizeLimitExceededException; import org.apache.commons.fileupload.FileUploadException; import org.apache.commons.fileupload.disk.DiskFileItemFactory; import org.apache.commons.fileupload.servlet.ServletFileUpload; public class FileUploadServlet extends HttpServlet { public void init(ServletConfig servletConfig)throws ServletException { super.init(servletConfig); } public void destory() { super.destroy(); } public void doGet(HttpServletRequest request, HttpServletResponse response)throws IOException { doPost(request, response); } public void showMessage(String message, HttpServletResponse response) { try { PrintWriter out = response.getWriter(); out.print("<script type=""text/javascript"">alert(""" + message + """);</script>"); }catch(Exception e) { e.printStackTrace(); } } public void doPost(HttpServletRequest request, HttpServletResponse response)throws IOException { final long MAX_SIZE = 5 * 1024 * 1024; //上传文件最大不超过5MB //设置编码格式支持中文显示 request.setCharacterEncoding("UTF-8"); response.setContentType("text/html"); response.setCharacterEncoding("UTF-8"); //实例化一个硬盘工厂,用于创建ServletFileUpload对象 DiskFileItemFactory diskFileItemFactory = new DiskFileItemFactory(); diskFileItemFactory.setSizeThreshold(4096); //设置文件临时存放的内存大小为4KB,多余部分将临时存放在硬盘 diskFileItemFactory.setRepository(new File("D:/temp")); //设置文件临时存放目录 //用上述硬盘工厂实例化一个文件上传对象 ServletFileUpload servletFileUpload = new ServletFileUpload(diskFileItemFactory); servletFileUpload.setSizeMax(MAX_SIZE); //设置上传文件最大值 PrintWriter out = response.getWriter(); List fileList = new ArrayList(); try { fileList = servletFileUpload.parseRequest(request); //解决上传文件为空的错误 if(fileList == null || fileList.size() == 0) { showMessage("未选择文件,请选择!", response); return; } }catch(FileUploadException e) { //捕捉超过最大值的异常并显示错误提示 if(e instanceof SizeLimitExceededException) { showMessage("文件过大,无法上传!", response); return; } showMessage("上传失败,请检查网络状况或联系相关技术人员!", response); e.printStackTrace(); } Iterator fileIter = fileList.iterator(); //得到文件List的迭代器,以下进行保存文件操作 while(fileIter.hasNext()) { FileItem fileItem; fileItem = (FileItem) fileIter.next(); //忽略不是文件域的表单字段 if(fileItem == null || fileItem.isFormField()) { continue; } if(fileItem.getSize() != 0) { File newFile = new File("D:/images/" + fileItem.getName()); try { //保存文件 fileItem.write(newFile); }catch(Exception e) { showMessage("上传失败,请检查网络状况或联系相关技术人员!", response); e.printStackTrace(); } }else { showMessage("文件内容为空,请检查!", response); return; } showMessage("文件上传成功!", response); } } } 上传的核心代码 其他自己看着办吧 |
The Java EE v7.0 Tutorial 第17章 有这个例子。
|