//设置上传文件保存路径
String filePath = getServletContext().getRealPath("/") + "images";
System.out.println(filePath);
File file = new File(filePath);
if (!file.exists()) {
file.mkdirs();
}
SmartUpload su = new SmartUpload();
//初始化对象
su.initialize(getServletConfig(), request, response);
//设置上传文件大小
su.setMaxFileSize(1024*1024*10);//为10M
//设置所有文件的大小
su.setTotalMaxFileSize(1024*1024*100);//为100M
//设置允许上传文件类型
su.setAllowedFilesList("txt,jpg,gif,mp3");
String result = "上传成功";
//设置禁止上传文件类型
try {
su.setDeniedFilesList("rar,jsp,sj");
//上传文件
su.upload();
int count = su.save(filePath);
System.out.println("上传成功" + count + "个文件!");
} catch (Exception e) {
result = "上传失败";
e.printStackTrace();
}
request.setAttribute("result", result);
request.getRequestDispatcher("02.jsp").forward(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}