【WebMail】利用commons.fileupload组件进行文件上传

利用commons.fileupload组件进行文件上传,首先要把下面的两个包导入web工程

Servlet:

 

 

import java.io.File;
import java.io.IOException;
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.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

/**
 * @author $KangMiao$
 * 文件上传
 */
public class UploadServlet extends HttpServlet{

	@Override
	protected void service(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		try {
			req.setCharacterEncoding("UTF-8");
			resp.setCharacterEncoding("UTF-8");
			//获得一个文件上传对象
			DiskFileItemFactory factory = 
				new DiskFileItemFactory();
			ServletFileUpload sfu = 
			new ServletFileUpload(factory);
	    	//获得所有request请求
			List<FileItem> list = sfu.parseRequest(req);
			for (FileItem item : list) {
				if(item.isFormField()){  //判断参数是否为文件类型
					System.out.println("非文件类型数据:"
							+item.getFieldName()+"--"
							+item.getName());
				}else{
					System.out.println("name:"+item.getName()); //获得上传文件路径
					System.out.println("fileName:"+item.getFieldName());
					System.out.println("contentType:"+item.getContentType()); //获得上传文件类型
					System.out.println("size:"+item.getSize());  //上传文件大小
					
					String fromPath = item.getName();
					//获得保存文件的绝对路径
					String savePath = this.getServletContext().
					                  getRealPath("/")+"userfile";
					File fromFile = new File(fromPath);
					File toFile = new File(savePath,fromFile.getName());
					item.write(toFile);
					
 				}
			}
		} catch (FileUploadException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}

 

 

 

JSP:

 

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>文件上传</title>
    
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->

  </head>
  
  <body>
   <form action="upfile" method="post" enctype="multipart/form-data">
    <input type="text" name="user">
    文件上传:<input type="file" name="file1">
    <input type="file" name="file2">
    <input  type="submit" value="上传"> 
    </form>
    <br>
    <a href="downfile">文件列表</a>
  </body>
</html>

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值