apache上传html,Apache FileUpload的两种上传方式介绍及应用

环境:

tomcat5.6

commmons-fileupload-1.3.jar

commmons-io-2.4.jar

JSP

编码:UTF-8

临时文件夹:fileupload/tmp相对于网站根目录

上传文件保存位置:fileupload

Traditional API上传方式

//fileload01.htm

File to upload:

to upload the file!

//traditionalapi.jsp

request.setCharacterEncoding("UTF-8");

// file less than 10kb will be store in memory, otherwise in file system.

final int threshold = 10240;

final File tmpDir = new File(getServletContext().getRealPath("/") + "fileupload" + File.separator + "tmp");

final int maxRequestSize = 1024 * 1024 * 4; // 4MB

// Check that we have a file upload request

if(ServletFileUpload.isMultipartContent(request))

{

// Create a factory for disk-based file items.

FileItemFactory factory = new DiskFileItemFactory(threshold, tmpDir);

// Create a new file upload handler

ServletFileUpload upload = new ServletFileUpload(factory);

// Set overall request size constraint.

upload.setSizeMax(maxRequestSize);

List items = upload.parseRequest(request); // FileUploadException

for(FileItem item : items)

{

if(item.isFormField()) //regular form field

{

String name = item.getFieldName();

String value = item.getString();

%>

-->

}

else

{ //file upload

String fieldName = item.getFieldName();

String fileName = item.getName();

File uploadedFile = new File(getServletContext().getRealPath("/") +

"fileupload" + File.separator + fieldName + "_" + fileName);

item.write(uploadedFile);

%>

upload file done!

}

}

}

%>

Streaming API上传方式

//fileupload02.htm

File to upload:

to upload the file!

//streamingapi.jsp

request.setCharacterEncoding("UTF-8");

// Check that we have a file upload request

if(ServletFileUpload.isMultipartContent(request))

{

// Create a new file upload handler

ServletFileUpload upload = new ServletFileUpload();

// Parse the request

FileItemIterator iter = upload.getItemIterator(request);

while(iter.hasNext())

{

FileItemStream item = iter.next();

String fieldName = item.getFieldName();

InputStream is = item.openStream();

if(item.isFormField()) //regular form field

{

%>

-->

}

else

{ //file upload

String fileName = item.getName();

File uploadedFile = new File(getServletContext().getRealPath("/") +

"fileupload" + File.separator + fieldName + "_" + fileName);

OutputStream os = new FileOutputStream(uploadedFile);

// write file to disk and close outputstream.

Streams.copy(is, os, true);

%>

upload file done!

}

}

}

%>

Traditional API vs Streaming API

Streaming API上传速度相对较快。因为它是利用内存保存上传的文件,节省了传统API将文件写入临时文件带来的开销。

可参考:

http://stackoverflow.com/questions/11620432/apache-commons-fileupload-streaming-api

This page describes the traditional API of the commons fileupload library. The traditional API is a convenient approach. However, for ultimate performance, you might prefer the faster Streaming API.

http://commons.apache.org/proper/commons-fileupload/using.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值