资源简介
使用Apache 的 Commons FileUpload 实现上传功能。代码中包含两种上传方式,普通上传和jquery后台上传。
代码片段和文件信息
package com.upload;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
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 JqueryFileUploadServlet extends HttpServlet {
/**
*
*/
private static final long serialVersionUID = 1L;
/**
* The doPost method of the servlet.
*
* 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 {
response.setCharacterEncoding(“UTF-8“); // 设置处理请求的编码方式
// 在Response头中指定Content-Type为text/html,而并非text/html,是可以解决问题的。这样返回给客户端的是一个JSON字符串(并非JSON对象)
// ,无需IE来解析。随后,使用JSON.parse()函数将JSON字符串解析为JSON对象即可。
// response.setContentType(“application/json;charset=UTF-8“); // 设置Content-Type字段值
response.setContentType(“text/html;charset=UTF-8“); // 设置Content-Type字段值
PrintWriter out = response.getWriter();
// 实例化一个硬盘文件工厂,用来配置文件上传组件ServletFileUpload
FileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(factory); // 利用硬盘文件工厂配置文件上传组件
List items = null; // 存放FileItem对象
try { // 获取文件的FileItem对象,即表单域分为普通表单域和文件域
items = upload.parseRequest(request);
} catch (FileUploadException e) {
e.printStackTrace();
}
String path = null; // 存放上传文件的完整名称,包括路径。
String filename = “ “; // 存放文件名
InputStream is = null;
// 循环处理上传文件
for (FileItem item : items) {
if (item.isFormField()) {
if (item.getFieldName().equals(“filename“)) {
if (!item.getString().equals(““))
filename = item.getString(“UTF-8“);
}
} else if (item.getName().trim() != null
&& !item.getName().trim().equals(“ “)) {
path = item.getName();// 得到文件完整路径
filename = path.substring(path.lastIndexOf(“\\“) + 1);
is = item.getInputStream(); // 获得上传文件的InputStream对象
}
}
String pathName = request.getRealPath(“/“) + filename;
if (new File(pathName).exists()) {
// out.println(“该文件已经存在,请为文件指定一个新的文件名!“);
out.println(“{\“Result\“:\“-2\“}“);
} else if (!pathName.equals(““)) {
FileOutputStream fos = ne
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
文件 526 2015-01-30 17:38 uploadDemo\.classpath
文件 303 2015-01-30 17:33 uploadDemo\.myme
文件 1228 2015-01-30 17:35 uploadDemo\.project
文件 3908 2015-02-28 15:32 uploadDemo\src\com\upload\JqueryFileUploadServlet.java
文件 3414 2015-02-28 14:04 uploadDemo\src\com\upload\NormalFileUploadServlet.java
文件 2727 2015-02-28 13:55 uploadDemo\WebRoot\css\WizardWuUpload_Blue.css
文件 2621 2015-02-28 13:55 uploadDemo\WebRoot\css\WizardWuUpload_Green.css
文件 2641 2015-02-28 13:55 uploadDemo\WebRoot\css\WizardWuUpload_Silver.css
文件 511 2015-02-28 15:03 uploadDemo\WebRoot\index.jsp
文件 3092 2015-02-28 15:40 uploadDemo\WebRoot\jqupload_single.jsp
文件 94840 2015-02-28 13:55 uploadDemo\WebRoot\js\jquery-1.7.2.min.js
文件 39718 2015-02-28 13:55 uploadDemo\WebRoot\js\jquery.form.js
文件 17898 2015-02-28 15:39 uploadDemo\WebRoot\js\json2.js
文件 7917 2015-02-28 15:58 uploadDemo\WebRoot\js\WizardWuUpload.js
文件 39 2015-01-30 17:33 uploadDemo\WebRoot\me
文件 748 2015-02-28 14:05 uploadDemo\WebRoot\normal.jsp
文件 3971 2015-02-28 15:32 uploadDemo\WebRoot\WEB-INF\classes\com\upload\JqueryFileUploadServlet.class
文件 3963 2015-02-28 14:04 uploadDemo\WebRoot\WEB-INF\classes\com\upload\NormalFileUploadServlet.class
文件 69002 2015-01-30 17:38 uploadDemo\WebRoot\WEB-INF\lib\commons-fileupload-1.3.1.jar
文件 185140 2015-01-30 17:38 uploadDemo\WebRoot\WEB-INF\lib\commons-io-2.4.jar
文件 1005 2015-02-28 14:06 uploadDemo\WebRoot\WEB-INF\web.xm
目录 0 2015-02-28 14:06 uploadDemo\WebRoot\WEB-INF\classes\com\upload
目录 0 2015-02-12 09:32 uploadDemo\WebRoot\WEB-INF\classes\com
目录 0 2015-02-28 14:06 uploadDemo\src\com\upload
目录 0 2015-02-12 09:32 uploadDemo\WebRoot\WEB-INF\classes
目录 0 2015-01-30 17:38 uploadDemo\WebRoot\WEB-INF\lib
目录 0 2015-01-30 17:57 uploadDemo\src\com
目录 0 2015-02-28 13:55 uploadDemo\WebRoot\css
目录 0 2015-02-28 15:39 uploadDemo\WebRoot\js
目录 0 2015-01-30 17:33 uploadDemo\WebRoot\me
............此处省略8个文件信息
评论
共有 条评论