comm-upload 多文件上传重名 文件丢失问题

starnight_cbj 2009-03-26 10:47:43


package example;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
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.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

/**
* 上传servlet
*
* @author Administrator
*
*/
public class Upload extends HttpServlet {

public Upload() {

}

public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

DiskFileItemFactory factory = new DiskFileItemFactory();
String path = getServletContext().getRealPath("/") + "upload/";// 取得站台文件夹路径
factory.setRepository(new File(path));
factory.setSizeThreshold(2 * 1024 * 1024);
ServletFileUpload upload = new ServletFileUpload(factory);
upload.setHeaderEncoding("utf-8");

try {
List<FileItem> list = upload.parseRequest(request);

for (FileItem item : list) {
if (item.isFormField()) { // 如果是表单域,不是文件域
String name = item.getFieldName();
String value = item.getString("UTF-8");
} else {
String name = item.getFieldName();// 取得文件域的名称(name)
String value = item.getName();// (表单值)
if (!"".equals(value)) {// 文件域不为空的上传

String fileName = getFileName(value);// 传入值取得文件名
String fileType = getExt(fileName);// 取得后缀

String fileNewName = String.valueOf(System
.currentTimeMillis())
+ "." + fileType;// 以时间命名

System.out.println("文件名 : " + fileName);
System.out.println("文件类型 : " + fileType);

// Thread.sleep(200);//这里停下时间,问题解决,但感觉不好!!
File saveFile = new File(path, fileNewName);
// File saveFile = new File(path, fileName);
item.write(saveFile);

}
}
}
} catch (FileUploadException e) {

System.out.println("文件上传出错" + e.getMessage());
e.printStackTrace();
} catch (Exception e) {
System.out.println("写入硬盘出错" + e.getMessage());
e.printStackTrace();
}
response.getWriter().print("Upload OK!");
}

/**
* 取得文件名
*
* @param fieldValue
* @return
*/
private String getFileName(String fieldValue) {

int fileNameIndex = fieldValue.lastIndexOf("\\");// 最后斜杠的位置
String fieldName = fieldValue.substring(fileNameIndex + 1);

return fieldName;
}

/**
* 取得文件后缀
*
* @param fileName
* @return
*/
private String getExt(String fileName) {
String type = fileName.substring(fileName.lastIndexOf(".") + 1);
return type;
}

}

...全文
187 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
starnight_cbj 2009-03-27
  • 打赏
  • 举报
回复
OK了!!
starnight_cbj 2009-03-27
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 foolishdault 的回复:]
上传保存文件时文件名添加一个时间戳可以解决此问题。
[/Quote]
我上面的不也重名了吗?可以说具体点吗?
铁匠梁 2009-03-26
  • 打赏
  • 举报
回复
上传保存文件时文件名添加一个时间戳可以解决此问题。
starnight_cbj 2009-03-26
  • 打赏
  • 举报
回复
一定要上传之前处理?在上传时处理不可以??
function_chk 2009-03-26
  • 打赏
  • 举报
回复
上传之件之前就先把文件名重写掉
starnight_cbj 2009-03-26
  • 打赏
  • 举报
回复
怎么还没有人回答的?应该很简单吧!!!不过我不会!

81,077

社区成员

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

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