use of com.jspsmart.upload.SmartUpload in project HeartWatchdogJsp by Viczyf.
the class FileServlet method addFile.
public void addFile(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// String filePath = "upload\\\\file\\\\";
String filePath = "upload/file/";
String messages = "";
String forward = "";
String href = "";
FileDao fileDao = new FileDao();
FileBean fileBean = new FileBean();
SmartUpload su = new SmartUpload();
long maxsize = 2097152L;
HttpSession session = request.getSession();
User user = (User) session.getAttribute("user");
int id = user.getUid();
try {
su.initialize(this.getServletConfig(), request, response);
su.setMaxFileSize(maxsize);
su.setAllowedFilesList("jpg,gif,png,txt,doc,pdf,docx");
su.upload();
String fileinfo = su.getRequest().getParameter("info");
if (fileinfo != null && !fileinfo.equals("")) {
com.jspsmart.upload.File file = su.getFiles().getFile(0);
if (!file.isMissing()) {
String fileAddr = filePath + id + "_" + fileDao.queryMaxId() + "." + file.getFileExt();
System.out.println(fileAddr);
String now = MyTools.changeTime(new Date());
fileBean.setUid(id);
fileBean.setFileAddr(fileAddr);
fileBean.setFileDate(now);
fileBean.setFileInfo(fileinfo);
boolean mark = fileDao.operationFile("upload", fileBean);
if (mark) {
try {
file.saveAs(fileAddr, 1);
messages = "上传文件成功!";
forward = "tishi.jsp";
href = "fileupload.jsp";
} catch (SmartUploadException var18) {
messages = "上传文件失败!";
forward = "tishi.jsp";
href = "fileupload.jsp";
var18.printStackTrace();
}
} else {
messages = "保存文件信息失败!";
forward = "tishi.jsp";
href = "fileupload.jsp";
}
} else {
messages = "请选择要上传的文件!";
forward = "tishi.jsp";
href = "fileupload.jsp";
}
} else {
messages = "请输入照片描述信息!";
forward = "tishi.jsp";
href = "fileupload.jsp";
}
} catch (SecurityException var19) {
messages = "<li>上传文件失败!上传的文件类型只允许为:jpg,gif,png,txt,doc,docx,pdf;且文件大小不超过2MB</li>";
forward = "tishi.jsp";
href = "fileupload.jsp";
} catch (SmartUploadException var20) {
messages = "上传文件失败!";
forward = "tishi.jsp";
href = "fileupload.jsp";
var20.printStackTrace();
}
request.setAttribute("message", messages);
request.setAttribute("href", href);
RequestDispatcher requestDispatcher1 = request.getRequestDispatcher(forward);
requestDispatcher1.forward(request, response);
}
Aggregations