HTML 代码:
<div class="input-chunk"> <div>输入文件:</div> <input type="file" value="选择文件" id="upload-file"> <br> <a id="start-upload" href="javascript:void(0);" onclick="uploadFile();">开始上传</a> </div>
js代码:
function uploadFile(){ var fileObj = document.getElementById("upload-file").files[0]; // 获取文件对象 var FileController = "entityServlet1"; // 接收上传文件的后台地址 if(fileObj){ alert(fileObj); // FormData 对象 var form = new FormData(); form.append("file", fileObj);// 文件对象 // XMLHttpRequest 对象 var xhr = new XMLHttpRequest(); xhr.open("post", FileController, true); xhr.onload = function () { alert(xhr.responseText); }; xhr.send(form); }else{ alert("未选择文件"); } }
Java Servlet 代码:
import java.io.File; import java.io.IOException; 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.disk.DiskFileItemFactory; import org.apache.commons.fileupload.servlet.ServletFileUpload; public class EntityServlet1 extends HttpServlet { private static final long serialVersionUID = 1L; private String uploadPath = "D:\temp"; // 上传文件的目录 File tempPathFile; // 重写doPost方法,处理事件识别请求 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { try { // Create a factory for disk-based file items DiskFileItemFactory factory = new DiskFileItemFactory(); // Set factory constraints factory.setSizeThreshold(4096); // 设置缓冲区大小,这里是4kb factory.setRepository(tempPathFile);// 设置缓冲区目录 // Create a new file upload handler ServletFileUpload upload = new ServletFileUpload(factory); // Set overall request size constraint upload.setSizeMax(4194304); // 设置最大文件尺寸,这里是4MB List<FileItem> items = upload.parseRequest(request);// 得到所有的文件 Iterator<FileItem> i = items.iterator(); while (i.hasNext()) { FileItem fi = (FileItem) i.next(); String fileName = fi.getName(); if (fileName != null) { File fullFile = new File(new String(fi.getName().getBytes(), "utf-8")); // 解决文件名乱码问题 File savedFile = new File(uploadPath, fullFile.getName()); fi.write(savedFile); } } System.out.print("upload succeed"); } catch (Exception e) { } } }