java 上传小实例

前端jsp

<html>
<head>
<meta http-equiv="Content-Type" contect="text/html;charset=utf-8">
</head>
<body>
<form action="../fileupload/upload" method="post"
enctype="multipart/form-data">
<!-- file对应的input必须有name属性,否则不能上传 -->
<input type="file" name="file"/>
<input type="submit" value="submit" />
</form>
</body>
</html>


后台servlet

package org.xxm;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import dareway.org.apache.commons.fileupload.FileItem;
import dareway.org.apache.commons.fileupload.FileUploadBase.SizeLimitExceededException;
import dareway.org.apache.commons.fileupload.disk.DiskFileItemFactory;
import dareway.org.apache.commons.fileupload.servlet.ServletFileUpload;
import dareway.org.apache.commons.fileupload.util.Streams;

/**
* @author xuxiaoming
* 2009-03-19
* 以tomcat6.0为服务器
*/
public class UploadFileServlet extends HttpServlet {
public void doGet(HttpServletRequest req,
HttpServletResponse res) {
return;
}

public void doPost(HttpServletRequest req,
HttpServletResponse res) throws IOException {
/**
* form中的enctype必须是multipart/...
* 组件提供方法检测form表单的enctype属性
* 在isMultipartContent方法中同时检测了是否是post提交
* 如果不是post提交则返回false
*/
if(ServletFileUpload.isMultipartContent(req)) {
req.setCharacterEncoding("utf-8");
DiskFileItemFactory factory = new DiskFileItemFactory();
/**
* 临时文件存储路径要真实存在
*/
factory.setRepository(new File("../webapps/fileupload/tmp"));
//内存最大占用
factory.setSizeThreshold(1024000);
ServletFileUpload sfu = new ServletFileUpload(factory);
//单个文件最大值byte
sfu.setFileSizeMax(102400000);
//所有上传文件的总和最大值byte
sfu.setSizeMax(204800000);
List items = null;
try {
items = sfu.parseRequest(req);
} catch (SizeLimitExceededException e) {
System.out.println("size limit exception!");
} catch(Exception e) {
e.printStackTrace();
}

Iterator iter = items==null?null:items.iterator();
while(iter != null && iter.hasNext()) {
FileItem item = (FileItem)iter.next();
//简单的表单域
if(item.isFormField()) {
System.out.print("form field:");
System.out.print(item.getFieldName() + " ");
System.out.print(item.getString());
}
//文件域
else if(!item.isFormField()) {
System.out.println("client name:" + item.getName());
String fileName = item.getName().substring(item.getName().lastIndexOf("\\"));
BufferedInputStream in = new BufferedInputStream(item.getInputStream());
//文件存储在工程的upload目录下
BufferedOutputStream out = new BufferedOutputStream(
new FileOutputStream(new File("../webapps/fileupload/upload/" + fileName)));
Streams.copy(in, out, true);
}
}
} else {
System.out.println("enctype error!");
}
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值