import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Iterator;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
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;
/**
*
* @author zhupan
*
*/
public class Upload extends HttpServlet {
/**
*
*/
private static final long serialVersionUID = 1L;
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html; charset=GB2312");
PrintWriter out = response.getWriter();
try {
DiskFileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload sevletFileUpload = new ServletFileUpload(factory);
// 设置允许用户上传文件大小,单位:字节,这里设为2m
sevletFileUpload.setSizeMax(2 * 1024 * 1024);
// 设置最多只允许在内存中存储的数据,单位:字节
factory.setSizeThreshold(4096);
// 设置一旦文件大小超过getSizeThreshold()的值时数据存放在硬盘的目录
factory.setRepository(new File("d:\\temp"));
// 开始读取上传信息
List fileItems = sevletFileUpload.parseRequest(request);
// 依次处理每个上传的文件
Iterator iter = fileItems.iterator();
// 正则匹配,过滤路径取文件名
String regExp = ".+\\\\(.+)$";
// 过滤掉的文件类型
String[] errorType = { ".exe", ".com", ".cgi", ".asp" };
Pattern p = Pattern.compile(regExp);
while (iter.hasNext()) {
FileItem item = (FileItem) iter.next();
// 忽略其他不是文件域的所有表单信息
if (!item.isFormField()) {
String name = item.getName();
long size = item.getSize();
if ((name == null || name.equals("")) && size == 0)
continue;
Matcher m = p.matcher(name);
boolean result = m.find();
if (result) {
for (int temp = 0; temp < errorType.length; temp++) {
if (m.group(1).endsWith(errorType[temp])) {
throw new IOException(name + ": 非法文件类型禁止上传");
}
}
try {
// 保存上传的文件到指定的目录
// 在下文中上传文件至数据库时,将对这里改写开始
item.write(new File("d:\\uploadfile\\"+ m.group(1)));
out.print(name + " " + size + "<br>");
// 在下文中上传文件至数据库时,将对这里改写结束
} catch (Exception e) {
out.println(e);
}
} else {
throw new IOException("fail to upload");
}
}
}
} catch (IOException e) {
out.println(e);
} catch (FileUploadException e) {
out.println(e);
}
}
}
item.write(new File("d:\\uploadfile\\"+ m.group(1)));
这个地方是怎么改成虚拟路径我改成("../uploadfile")就说找不到该文件夹
急用,我是刚注册的不知道能不能给大家加分
4 个解决方案
#1
应该是路径不对,你在前面加入这样的代码试试
//获得服务器应用程序所在的绝对路径
String realPath = this.getServletContext().getRealPath(this.
getServletName());
realPath = realPath.substring(0, realPath.lastIndexOf("\\"));
String uploadPath = realPath + "\\upload\\"; // 用于存放上传文件的目录
//如果目录不存在则创建目录
File uploadPathDir = new File(uploadPath);
if (!uploadPathDir.exists()) {
uploadPathDir.mkdirs();
}
试试看
//获得服务器应用程序所在的绝对路径
String realPath = this.getServletContext().getRealPath(this.
getServletName());
realPath = realPath.substring(0, realPath.lastIndexOf("\\"));
String uploadPath = realPath + "\\upload\\"; // 用于存放上传文件的目录
//如果目录不存在则创建目录
File uploadPathDir = new File(uploadPath);
if (!uploadPathDir.exists()) {
uploadPathDir.mkdirs();
}
试试看
#2
怎么老说我总分和不对,晕了
谢谢你了
谢谢你了
#3
给分的总数一定和帖子的数相等
#4
给分了,谢谢你们
#1
应该是路径不对,你在前面加入这样的代码试试
//获得服务器应用程序所在的绝对路径
String realPath = this.getServletContext().getRealPath(this.
getServletName());
realPath = realPath.substring(0, realPath.lastIndexOf("\\"));
String uploadPath = realPath + "\\upload\\"; // 用于存放上传文件的目录
//如果目录不存在则创建目录
File uploadPathDir = new File(uploadPath);
if (!uploadPathDir.exists()) {
uploadPathDir.mkdirs();
}
试试看
//获得服务器应用程序所在的绝对路径
String realPath = this.getServletContext().getRealPath(this.
getServletName());
realPath = realPath.substring(0, realPath.lastIndexOf("\\"));
String uploadPath = realPath + "\\upload\\"; // 用于存放上传文件的目录
//如果目录不存在则创建目录
File uploadPathDir = new File(uploadPath);
if (!uploadPathDir.exists()) {
uploadPathDir.mkdirs();
}
试试看
#2
怎么老说我总分和不对,晕了
谢谢你了
谢谢你了
#3
给分的总数一定和帖子的数相等
#4
给分了,谢谢你们