使用common-fileupload框架实现文件上传

到http//jakarta.apache.org/commons/fileupload站点下载commons-fileupload-1.2.1-bin
到http//jakarta.apache.org/commons/io站点下载commons-io-1.4-bin
同时将两个文件解压开后,将commons-io-1.4.jar,commons-fileupload-1.2.1.jar文件拷贝到tomcat服务器Lib文件夹下面
index.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> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> <form action="pro2.jsp" enctype="multipart/form-data" method="post"> 上传文件:<input type="file" name="file" /><br> 请求参数:<input type="text" name="wawa" /><br> <input type="submit" name="dd" value="提交"> </form> </body> </html>

pro2.jsp页面
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ page import="org.apache.commons.fileupload.disk.*,org.apache.commons.fileupload.*" %>
<%@ page import="org.apache.commons.fileupload.servlet.*, java.io.* "  %>
<%@ page import="java.util.*"%>
<%
	DiskFileItemFactory factory = new DiskFileItemFactory();
	factory.setSizeThreshold(1024*1024*20);
	factory.setRepository(new File(request.getRealPath("/")));
	ServletFileUpload upload = new ServletFileUpload(factory);
	upload.setSizeMax(1024*1024*20);
	List items = upload.parseRequest(request);
	for(Iterator it=items.iterator(); it.hasNext();){
		FileItem item = (FileItem)it.next();
		//判断是否为表单域
		if(item.isFormField()){
			String name = item.getFieldName();
			String value = item.getString("UTF-8");
			out.println("name=value:"+"name="+name+",value="+value);
		}else {
			//获取文件域名
			String fieldName = item.getFieldName();
			//获取文件名
			String fileName = item.getName();
			//获得文件类型
			String contentType = item.getContentType();
			FileOutputStream fos = new FileOutputStream(request.getRealPath("/") + System.currentTimeMillis()+
			fileName.subSequence(fileName.indexOf("."), fileName.length()));
			if(item.isInMemory()){
				fos.write(item.get());
			}else {
				InputStream is = item.getInputStream();
				byte [] buffer = new byte[1024];
				int len;
				while((len = is.read(buffer)) >1){
					fos.write(buffer, 0, len);
				}
				is.close();
				fos.close();
			}
			
		}
	}
	
%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>使用common-fileupdate框架上传文件</title>
</head>
<body>

</body>
</html>

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值