(在线)使用jupload来上传多文件

whmjxa 2008-12-30 09:56:00
如题,哪位仁兄做过类似的东东,用Jupload来上传多文件,界面是用applet来实现的
在线急求
...全文
395 16 打赏 收藏 转发到动态 举报
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
xianyiwuhappy 2011-10-17
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 whmjxa 的回复:]
再解释一下,是要能上传文件夹的,重复一遍,不仅仅是多文件,还要支持上传文件夹
[/Quote]

我也有同样的需求啊
软件开发者 2011-08-11
  • 打赏
  • 举报
回复
很是无语,上传时怎么选择文件夹,然后再选择要上传的文件(两个选择框---第一个选择文件夹,第二个选择文件),怎么实现???????????????、、
firewing_king 2009-03-30
  • 打赏
  • 举报
回复
学习学习
wula0010 2008-12-31
  • 打赏
  • 举报
回复
呵呵,多文件上传的我基本实现了,还要优化一下,如果有兴趣,可以关注这个:http://topic.csdn.net/u/20081230/12/8e367d10-a63e-4d58-ba28-5f3f9777806a.html?seed=599575251

我的不能传文件夹,<file>只能选择文件吧,........
guolimin1118 2008-12-31
  • 打赏
  • 举报
回复
顶下
还没有见过这么强的需求
连文件件也能上传
yqsshr 2008-12-31
  • 打赏
  • 举报
回复
up
whmjxa 2008-12-31
  • 打赏
  • 举报
回复
再解释一下,是要能上传文件夹的,重复一遍,不仅仅是多文件,还要支持上传文件夹
whmjxa 2008-12-31
  • 打赏
  • 举报
回复
up up up up
wula0010 2008-12-30
  • 打赏
  • 举报
回复
和我的问题一样,........


纯属巧合.............
kokobox 2008-12-30
  • 打赏
  • 举报
回复
晚上---》网上
kokobox 2008-12-30
  • 打赏
  • 举报
回复
以前我回复过这个问题,现在找不到了

你可以直接用file-upload 来做

用struts的话只是在ActionForm中做一点手脚,放一个list进去

这个不难,晚上也有很多资料,搜一下吧
whmjxa 2008-12-30
  • 打赏
  • 举报
回复
额,可能我说的不是很清楚,那我再把问题说一遍,是一次性上传多个文件,而不是通过页面上有多个file的属性框来上传多个文件
montao 2008-12-30
  • 打赏
  • 举报
回复

下面是个例子,你可以看看:




servlet类:






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

public class Upload extends HttpServlet {
File fileDir;
File tempDir;

/**
* Constructor of the object.
*/
public Upload() {
super();
}

/**
* Destruction of the servlet. <br>
*/
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}

/**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
*
* @param request
* the request send by the client to the server
* @param response
* the response send by the server to the client
* @throws ServletException
* if an error occurred
* @throws IOException
* if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// 让不同的请求做相同的事情
this.doPost(request, response);
}

/**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to
* post.
*
* @param request
* the request send by the client to the server
* @param response
* the response send by the server to the client
* @throws ServletException
* if an error occurred
* @throws IOException
* if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// Create a factory for disk-based file items
DiskFileItemFactory factory = new DiskFileItemFactory();
factory.setSizeThreshold(1024 * 1024);
// 设置临时文件存储位置
factory.setRepository(tempDir);
ServletFileUpload upload = new ServletFileUpload(factory);
// 设置单个文件的最大上传size
upload.setFileSizeMax(1024 * 1024);
// 设置整个request的最大size
upload.setSizeMax(1024 * 1024);

ProgressListener progressListener = new ProgressListener() {
private long megaBytes = -1;

public void update(long pBytesRead, long pContentLength, int pItems) {
long mBytes = pBytesRead / 1000;
if (megaBytes == mBytes) {
return;
}
megaBytes = mBytes;
System.out.println("We are currently reading item " + pItems);
if (pContentLength == -1) {
System.out.println("So far, " + pBytesRead
+ " bytes have been read.");

} else {
System.out.println("So far, " + pBytesRead + " of "
+ pContentLength + " bytes have been read.");
}
}
};
upload.setProgressListener(progressListener);

// Parse the request
try {
List<FileItem> items = upload.parseRequest(request);
Iterator<FileItem> iter = items.iterator();
while (iter.hasNext()) {
FileItem item = iter.next();
if (item.isFormField()) {
processFormField(item);
} else {
processUploadedFile(item);
}
}
} catch (Exception e) {
e.printStackTrace();
}
response.setContentType("text/html");
}

// Handle the field submitted from client
private void processFormField(FileItem item) {
String name = item.getFieldName();
String value = item.getString();
try {
System.out.println("[name]\t" + name + "\t[value]\t"
+ new String(value.getBytes("iso8859-1"), "gbk"));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}

}

private String getFileName(String fileName) {
int index = fileName.lastIndexOf('\\');
if (index == -1) {
index = fileName.lastIndexOf('/');
}
if (index == -1) {
return fileName;
}
if (index == fileName.length() - 1) {
return fileName;
}
return fileName.substring(index + 1);

}

private void logInfo(Object o) {
System.out.println(o);
}

private void processUploadedFile(FileItem item) {
if (item.isFormField()) {
return;
}
String fieldName = item.getFieldName();
logInfo("[fieldName]\t" + fieldName);
String fileName = item.getName();
logInfo("[fileName]\t" + fileName);
String parsedFileName = getFileName(fileName);
logInfo("[parsedFileName]\t" + parsedFileName);
String contentType = item.getContentType();
logInfo("[contentType]\t" + contentType);
boolean isInMemory = item.isInMemory();
logInfo("[isInMemory]\t" + isInMemory);
long sizeInBytes = item.getSize();
logInfo("[sizeInBytes]\t" + sizeInBytes);
File uploadedFile = new File(fileDir, parsedFileName);
if (uploadedFile.exists()) {
// 如果文件同名做相应的处理
}
try {
item.write(uploadedFile);
} catch (Exception e) {
e.printStackTrace();
}
}

/**
* Initialization of the servlet. <br>
*
* @throws ServletException
* if an error occurs
*/
public void init() throws ServletException {
// 得到当前目录
fileDir = new File(this.getServletContext().getRealPath("/")
+ "WEB-INF\\upload\\");
// 如果WEB-INF下upload文件夹不存在,就新建一个
if (!fileDir.exists()) {
fileDir.mkdir();
}
tempDir = new File(this.getServletContext().getRealPath("/")
+ "WEB-INF\\uploadTemp\\");
if (!tempDir.exists()) {
tempDir.mkdir();
}
}
}




jsp页面:



<form method="post" action="upload" name="fileuploadform" enctype="multipart/form-data">
<input type="file" name="file"></input>
<input type="submit"/>
</form>


lanzhengwu 2008-12-30
  • 打赏
  • 举报
回复
帮顶..
whmjxa 2008-12-30
  • 打赏
  • 举报
回复
额,我再解释一下,不是用List来实现,而是用applet来实现,不是通过在页面上写多个file的属性框来实现
wula0010 2008-12-30
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 kokobox 的回复:]
以前我回复过这个问题,现在找不到了

你可以直接用file-upload 来做

用struts的话只是在ActionForm中做一点手脚,放一个list进去

这个不难,晚上也有很多资料,搜一下吧
[/Quote]

我也觉得应该这样做,但是网上没有找到啊,.........

81,092

社区成员

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

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