Common FileUpload组件的使用!(源码)
今天搞了一下文件上传功能组件Common FileUpload。
需要两个Jar包分别是commons-io-1.4.jar,commons-fileupload-1.2.1.jar。点击下载
只需要写一个上传类UploadServlet.java,还需要两个Jsp界面,分别负责上传文件和输出上传是否成功。
还需要在根目录下新建一个文件夹uploads,用于保存上传后文件。
下面是上传Servlet类源码:
package com.sy.web;
import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
import org.apache.commons.fileupload.*;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
public class UploadServlet extends HttpServlet {
private static final long serialVersionUID = -2201219701121037194L;
private String savePath;
private ServletContext sc;
public void init(ServletConfig config) {
savePath = config.getInitParameter("savePath");
sc = config.getServletContext();
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
DiskFileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(factory);
try {
List items = upload.parseRequest(request);// 上传文件解析
Iterator itr = items.iterator();// 枚举方法
while (itr.hasNext()) {
FileItem item = (FileItem) itr.next();
if (item.isFormField()) {// 判断是文件还是文本信息
System.out.println("表单参数名:" + item.getFieldName()
+ ",表单参数值:" + item.getString("UTF-8"));
} else {
if (item.getName() != null && !item.getName().equals("")) {// 判断是否选择了文件
System.out.println("上传文件的大小:" + item.getSize());
System.out.println("上传文件的类型:" + item.getContentType());
// item.getName()返回上传文件在客户端的完整路径名称
System.out.println("上传文件的名称:" + item.getName());
// 此时文件暂存在服务器的内存当中
File tempFile = new File(item.getName());// 构造临时对象
File file = new File(sc.getRealPath("/") + savePath,
tempFile.getName());
// 获取根目录对应的真实物理路径
item.write(file);// 保存文件在服务器的物理磁盘中
request.setAttribute("upload.message", "上传文件成功!");// 返回上传结果
} else {
request.setAttribute("upload.message", "没有选择上传文件!");
}
}
}
} catch (Exception e) {
e.printStackTrace();
request.setAttribute("upload.message", "上传文件失败!");
}
request.getRequestDispatcher("/uploadResult.jsp").forward(request,
response);
}
}
import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
import org.apache.commons.fileupload.*;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
public class UploadServlet extends HttpServlet {
private static final long serialVersionUID = -2201219701121037194L;
private String savePath;
private ServletContext sc;
public void init(ServletConfig config) {
savePath = config.getInitParameter("savePath");
sc = config.getServletContext();
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
DiskFileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(factory);
try {
List items = upload.parseRequest(request);// 上传文件解析
Iterator itr = items.iterator();// 枚举方法
while (itr.hasNext()) {
FileItem item = (FileItem) itr.next();
if (item.isFormField()) {// 判断是文件还是文本信息
System.out.println("表单参数名:" + item.getFieldName()
+ ",表单参数值:" + item.getString("UTF-8"));
} else {
if (item.getName() != null && !item.getName().equals("")) {// 判断是否选择了文件
System.out.println("上传文件的大小:" + item.getSize());
System.out.println("上传文件的类型:" + item.getContentType());
// item.getName()返回上传文件在客户端的完整路径名称
System.out.println("上传文件的名称:" + item.getName());
// 此时文件暂存在服务器的内存当中
File tempFile = new File(item.getName());// 构造临时对象
File file = new File(sc.getRealPath("/") + savePath,
tempFile.getName());
// 获取根目录对应的真实物理路径
item.write(file);// 保存文件在服务器的物理磁盘中
request.setAttribute("upload.message", "上传文件成功!");// 返回上传结果
} else {
request.setAttribute("upload.message", "没有选择上传文件!");
}
}
}
} catch (Exception e) {
e.printStackTrace();
request.setAttribute("upload.message", "上传文件失败!");
}
request.getRequestDispatcher("/uploadResult.jsp").forward(request,
response);
}
}
好了可以上传了。
施杨出品!!!
作者:Steven(Steven's Think out)
出处:http://shiyangxt.cnblogs.com
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。