日期:2014-05-16 浏览次数:20866 次
tomcat5.6
commmons-fileupload-1.3.jar
commmons-io-2.4.jar
JSP
编码:UTF-8
临时文件夹:fileupload/tmp相对于网站根目录
上传文件保存位置:fileupload
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <html> <body> <form method="POST" enctype="multipart/form-data" action="traditionalapi.jsp"> File to upload: <input type="file" name="file" size="40"><br/> <input type="submit" value="Press"> to upload the file! </form> </body> </html>
<%@page contentType="text/html;charset=UTF-8" pageEncoding="UTF-8" language="java"%> <%@page import="java.io.File"%> <%@page import="java.util.List"%> <%@page import="org.apache.commons.fileupload.*"%> <%@page import="org.apache.commons.fileupload.disk.DiskFileItemFactory"%> <%@page import="org.apache.commons.fileupload.servlet.ServletFileUpload"%> <% request.setCharacterEncoding("UTF-8"); // file less than 10kb will be store in memory, otherwise in file system. final int threshold = 10240; final File tmpDir = new File(getServletContext().getRealPath("/") + "fileupload" + File.separator + "tmp"); final int maxRequestSize = 1024 * 1024 * 4; // 4MB // Check that we have a file upload request if(ServletFileUpload.isMultipartContent(request)) { // Create a factory for disk-based file items. FileItemFactory factory = new DiskFileItemFactory(threshold, tmpDir); // Create a new file upload handler ServletFileUpload upload = new ServletFileUpload(factory); // Set overall request size constraint. upload.setSizeMax(maxRequestSize); List<FileItem> items = upload.parseRequest(request); // FileUploadException for(FileItem item : items) { if(item.isFormField()) //regular form field { String name = item.getFieldName(); String value = item.getString(); %> <h1><%=name%> --> <%=value%></h1> <% } else { //file upload String fieldName = item.getFieldName(); String fileName = item.getName(); File uploadedFile = new File(getServletContext().getRealPath("/") + "fileupload" + File.separator + fieldName + "_" + fileName); item.write(uploadedFile); %> <h1>upload file <%=uploadedFile.getName()%> done!</h1> <% } } } %>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <html> <body> <form method="POST" enctype="multipart/form-data" action="streamingapi.jsp"> File to upload: <input type="file" name="file" size="40"><br/> <input type="submit" value="Press"> to upload the file! </form> </body> </html>
<%@page contentType="text/html;charset=UTF-8" pageEncoding="UTF-8" language="java"%> <%@page import="java.io.*"%> <%@page import="