public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//创建一个SmartUpload的实例
SmartUpload upload = new SmartUpload();
try {
//初始化
upload.initialize(this.getServletConfig(), request, response);
// 限制每个上传文件的最大长度
upload.setMaxFileSize(10000);
//限制总上传数据的长度
upload.setTotalMaxFileSize(20000);
//设定允许上传的文件,仅允许doc,txt文件。
upload.setAllowedFilesList("doc,txt");
//禁止上传带有exe,bat扩展名的文件和没有扩展名的文件。
upload.setDeniedFilesList("exe,bat,jsp,htm,html,,");
//上传文件
upload.upload();
// /upload保存路径
upload.save("/upload");
//多文件加循环
com.jspsmart.upload.File file = upload.getFiles().getFile(0);
// 若文件不存在则继续
if (!file.isMissing()) {
//文件扩展名
String extname = file.getFileExt();
//产生一个唯一的文件名
String fileName = new Date().getTime() + "." + extname;
//保存路径
file.saveAs("/upload/"+fileName, upload.SAVE_VIRTUAL);
}
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* Initialization of the servlet. <br>
*
* @throws ServletException if an error occure
*/
public void init() throws ServletException {
// Put your code here
}
}