public class UpLoadAction extends ActionSupport implements ServletRequestAware,ServletResponseAware { private HttpServletRequest request; private HttpServletResponse response; private List<File> uploadFile; private List<String> uploadFileContentType; private List<String> uploadFileFileName; public void setServletRequest(HttpServletRequest request) { this.request = request; } public void setServletResponse(HttpServletResponse response) { this.response = response; } /** * 上传图片,至路径保存至数据库 * @return * @throws Exception */ public String uploadImage() throws Exception { String savePath = request.getSession().getServletContext().getRealPath("/upload/temp"); String result = "error"; File dir = new File(savePath); if (!dir.exists()) { dir.mkdirs(); } List<File> files = getUploadFile(); if((files!=null) && (files.size()>0)){ for (int i = 0; i < files.size(); i++) { FileUtils.copyFile(files.get(i), new File(savePath + "//" + getUploadFileFileName().get(i))); } } else{ return result; } return SUCCESS; } /** * 显示图片 * @return */ public String getHotelImage(){ try { String name = request.getParameter("name"); name = request.getSession().getServletContext().getRealPath("/") +"upload/hotel/" + name; FileInputStream fi = new FileInputStream(name); BufferedInputStream bi = new BufferedInputStream(fi); int c =0; while((c=bi.read())!=-1){ response.getWriter().write(c); } response.getWriter().flush(); fi.close(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } return "success"; } /** * 持久化到数据库 * @return */ public String insertiImageInfo() { try{ hotelimage = new HotelImage(); hotelimage.setImageTitle(imageTitle); String image = request.getParameter("uploadFileName"); hotelimage.setImageName(request.getParameter("oldUploadFileName")); String savePath = request.getSession().getServletContext().getRealPath("/upload"); try { hotelimage.setImagePath(URLEncoder.encode( image,"utf-8")); FileUtils.copyFile(new File(savePath+"/temp/" + hotelimage.getImagePath()), new File(savePath+"/hotel/" + hotelimage.getImagePath())); } catch (Exception e) { e.printStackTrace(); } HttpSession session = request.getSession(); //验证用户是否已登录 if(session.getAttribute("AdminSession")!=null){ UserInfo userInfo = (UserInfo) session.getAttribute("AdminSession"); if(userInfo!=null){ hotelimage.setUploadUserId(Integer.parseInt(userInfo.getId())); } } else{ hotelimage.setUploadUserId(0); } hotelimage.setHotelId(hotelId); hotelimage.setImageType(imageType); hotelimage.setImageDesc(content); int t = hotelImageService.addHotelImageService(hotelimage); if(t<=0){ return "error"; } hotelId="0"; imageType="0"; imageTitle=""; } catch(Exception ex){ ex.printStackTrace(); return "error"; } return "success"; } public String goEditImage(){ String id = request.getParameter("id"); HotelImage image = hotelImageService.getImageByID(Long.valueOf(id)); List<HotelInfo> hotels = hotelImageService.queryHotelListService(hotelInfo); request.setAttribute("hotels", hotels); request.setAttribute("image", image); if("audit".equals(request.getParameter("type"))){ return "audit"; } return "success"; } public String editImage() { try{ hotelimage = new HotelImage(); hotelimage.setImageTitle(imageTitle); String image = request.getParameter("uploadFileName"); hotelimage.setImageName(request.getParameter("oldUploadFileName")); String savePath = request.getSession().getServletContext().getRealPath("/upload"); try { hotelimage.setImagePath(UR