使用Apache的upload组件问题

cataclyam2000 2009-03-16 05:39:10
使用Apache的upload组件问题上传文件到服务器,由于文件是分块上传的,需要将多次上交的FileItem写入到一个文件中.由于上传文件很大,不能写入多个小文件后再一齐组装.服务器端的代码:
List /* FileItem */items = upload.parseRequest(request);
// Process the uploaded items
Iterator iter = items.iterator();
FileItem fileItem;
File fout;
out.println("[parseRequest.jsp] Let's read input files ...");
while (iter.hasNext()) {
fileItem = (FileItem) iter.next();

if (fileItem.isFormField()) {
out.println("[parseRequest.jsp] (form field) "
+ fileItem.getFieldName() + " = "
+ fileItem.getString());
// System.out.println("[parseRequest.jsp] (form field) "
// + fileItem.getFieldName() + " = "
// + fileItem.getString());

// If we receive the md5sum parameter, we've read
// finished to read the current file. It's not
// a very good (end of file) signal. Will be better in
// the future ... probably !
// Let's put a separator, to make output easier to read.
if (fileItem.getFieldName().equals("md5sum[]")) {
out
.println("[parseRequest.jsp] ------------------------------ ");
}
} else {
// Ok, we've got a file. Let's process it.
// Again, for all informations of what is exactly a
// FileItem, please
// have a look to
// http://jakarta.apache.org/commons/fileupload/
//
out.println("[parseRequest.jsp] FieldName: "
+ fileItem.getFieldName());
out.println("[parseRequest.jsp] File Name: "
+ fileItem.getName());
out.println("[parseRequest.jsp] ContentType: "
+ fileItem.getContentType());
out.println("[parseRequest.jsp] Size (Bytes): "
+ fileItem.getSize());
// If we are in chunk mode, we add ".partN" at the end
// of the file, where N is the chunk number.
String uploadedFilename = fileItem.getName()
+ (numChunk > 0 ? ".part" + numChunk : "");
fout = new File(ourTempDirectory
+ (new File(uploadedFilename)).getName());
out.println("[parseRequest.jsp] File Out: "
+ fout.toString());
// write the file
System.out.println("uploadedFilename:"+uploadedFilename);

fileItem.write(fout);


// FileInputStream fis = new FileInputStream(ourTempDirectory
// + fileItem.getName());
//
// FileOutputStream fos = new FileOutputStream(
// ourTempDirectory + fileItem.getName());
//
// byte[] byteBuff = new byte[1024];
// int nbBytes = 0;
// while ((nbBytes = fis.read(byteBuff)) >= 0) {
// // out.println("[parseRequest.jsp] " + " Nb
// // bytes read: " + nbBytes);
// fos.write(byteBuff, 0, nbBytes);
// }
// fis.close();
// fout.delete();
// receive(request, response);

// ////////////////////////////////////////////////////////////////////////////////////
// Chunk management: if it was the last chunk, let's
// recover the complete file
// by concatenating all chunk parts.
//
/*
if (bLastChunk) {
System.out.println("bLastChunk:"+bLastChunk);
out
.println("[parseRequest.jsp] Last chunk received: let's rebuild the complete file ("
+ fileItem.getName() + ")");
// First: construct the final filename.
FileInputStream fis;
FileOutputStream fos = new FileOutputStream(
ourTempDirectory + fileItem.getName());
int nbBytes;
byte[] byteBuff = new byte[1024];
String filename;
for (int i = 1; i <= numChunk; i += 1) {
filename = fileItem.getName() + ".part" + i;
out.println("[parseRequest.jsp] "
+ " Concatenating " + filename);
fis = new FileInputStream(ourTempDirectory
+ filename);
while ((nbBytes = fis.read(byteBuff)) >= 0) {
// out.println("[parseRequest.jsp] " + " Nb
// bytes read: " + nbBytes);
fos.write(byteBuff, 0, nbBytes);
}
fis.close();
}
fos.close();
}*/

// End of chunk management
// ////////////////////////////////////////////////////////////////////////////////////

fileItem.delete();
}
}// while
...全文
345 9 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
IThurricane 2009-05-05
  • 打赏
  • 举报
回复
学习学习
dinghun8leech 2009-05-05
  • 打赏
  • 举报
回复
我这边的情况是用iis转发到tomcat的,使用apache.common里的那个组件上传文件时只要文件一大于60k,必然会出什么分步上传啥啥啥的异常,害的我只能用最老土的方法从request里获取Servlet字节流来上传
superzhao123 2009-05-04
  • 打赏
  • 举报
回复
http://www.java2s.com/

jf
goodmrning 2009-05-03
  • 打赏
  • 举报
回复
先上传,再合并.
無名VF 2009-04-30
  • 打赏
  • 举报
回复
UP
Dantin 2009-04-30
  • 打赏
  • 举报
回复
先上传,再合并吧
East271536394 2009-04-30
  • 打赏
  • 举报
回复
package com.upload;

import com.jspsmart.upload.SmartUpload;
/*****
*
*author:East(张栋芳)
*date: 2008-7-19
*note: 上传文件和下载文件
*/

public class Upload {

/***
*
*上传文件平
*/
public void upLoad(){
// 新建一个SmartUpload对象
SmartUpload supload = new SmartUpload();
// 上传初始化
supload.initialize(pageContext);
// 限制每个上传文件的最大长度
supload.setMaxFileSize(10000);
// 限制总上传数据的长度。
supload.setTotalMaxFileSize(20000);
// 设定允许上传的文件(通过扩展名限制),仅允许doc,txt文件。
supload.setAllowedFilesList("doc,txt");
// 设定禁止上传的文件(通过扩展名限制),禁止上传带有exe,bat,
//jsp,htm,html扩展名的文件和没有扩展名的文件。
supload.setDeniedFilesList("exe,bat,jsp,html,htm");
// 上传文件
supload.upload();
// 将上传文件全部保存到指定目录
int count = supload.save("/upload");

com.jspsmart.upload.SmartFile file = supload.getFiles().getFile(0);
//文件名
String fileName = file.getFieldName();
}

/***
*
*下载文件
*/
public void downLoad(){
SmartUpload su = new SmartUpload();
su.initialize(pageContext);
// 设定contentDisposition为null以禁止浏览器自动打开文件,
//保证点击链接后是下载文件。若不设定,则下载的文件扩展名为
//doc时,浏览器将自动用word打开它。扩展名为pdf时,
//浏览器将用acrobat打开

su.setContentDisposition(null);
su.downloadFile("/upload/test.doc");
}

}
cataclyam2000 2009-03-23
  • 打赏
  • 举报
回复
120分就这样摆着也太浪费了,改为散分贴,大家给些java资源或者java论坛,网址吧.
swoky 2009-03-17
  • 打赏
  • 举报
回复
先上传,再合并吧
第1个上传组件commons-fileupload =============commons-fileupload ================ common-fileupload组件apache的一个开源项目之一,可以从http://jakarta.apache.org/commons/fileupload/下载。该组件简单易用,可实现一次上传一个或多个文件,并可限制文件大小。 -下载后解压zip包,将commons-fileupload-1.1.1.jar,和commons-io-1.2.jar(这里我们用的是更新的版本,但是用法是一样的)复制到tomcat的webapps\你的webapp\WEB-INF\lib\下,如果目录不存在请自建目录。 新建一个servlet: FileUpload.java用于文件上传: package com.drp.util.servlet; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.commons.fileupload.*; import java.util.*; import java.util.regex.*; import java.io.*; import org.apache.commons.fileupload.servlet.*; import org.apache.commons.fileupload.disk.DiskFileItemFactory; public class FileUpload extends HttpServlet { private String uploadPath = ""; // 用于存放上传文件的目录 private File tempPath = new File("D:\\Tomcat 5.5\\webapps\\drp1.2\\tempimages\\"); // 用于存放临时文件的目录 public void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { res.setContentType("text/html; charset=GB18030"); PrintWriter out = res.getWriter(); System.out.println(req.getContentLength()); System.out.println(req.getContentType()); DiskFileItemFactory factory = new DiskFileItemFactory(); // maximum size that will be stored in memory //允许设置内存中存储数据的门限,单位:字节 factory.setSizeThreshold(4096); // the location for saving data that is larger than getSizeThreshold() //如果文件大小大于SizeThreshold,则保存到临时目录 factory.setRepository(new File("D:\\Tomcat 5.5\\webapps\\drp1.2\\tempimages")); ServletFileUpload upload = new ServletFileUpload(factory); // maximum size before a FileUploadException will be thrown //最大上传文件,单位:字节 upload.setSizeMax(1000000); try { List fileItems = upload.parseRequest(req); // assume we know there are two files. The first file is a small //

62,634

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧