`

文件上传显示保存到数据库实现类

 
阅读更多
  1. publicclassUpLoadActionextendsActionSupportimplements
  2. ServletRequestAware,ServletResponseAware{
  3. privateHttpServletRequestrequest;
  4. privateHttpServletResponseresponse;
  5. privateList<File>uploadFile;
  6. privateList<String>uploadFileContentType;
  7. privateList<String>uploadFileFileName;
  8. publicvoidsetServletRequest(HttpServletRequestrequest){
  9. this.request=request;
  10. }
  11. publicvoidsetServletResponse(HttpServletResponseresponse){
  12. this.response=response;
  13. }
  14. /**
  15. *上传图片,至路径保存至数据库
  16. *@return
  17. *@throwsException
  18. */
  19. publicStringuploadImage()throwsException{
  20. StringsavePath=request.getSession().getServletContext().getRealPath("/upload/temp");
  21. Stringresult="error";
  22. Filedir=newFile(savePath);
  23. if(!dir.exists()){
  24. dir.mkdirs();
  25. }
  26. List<File>files=getUploadFile();
  27. if((files!=null)&&(files.size()>0)){
  28. for(inti=0;i<files.size();i++){
  29. FileUtils.copyFile(files.get(i),newFile(savePath+"//"
  30. +getUploadFileFileName().get(i)));
  31. }
  32. }
  33. else{
  34. returnresult;
  35. }
  36. returnSUCCESS;
  37. }
  38. /**
  39. *显示图片
  40. *@return
  41. */
  42. publicStringgetHotelImage(){
  43. try{
  44. Stringname=request.getParameter("name");
  45. name=request.getSession().getServletContext().getRealPath("/")+"upload/hotel/"+name;
  46. FileInputStreamfi=newFileInputStream(name);
  47. BufferedInputStreambi=newBufferedInputStream(fi);
  48. intc=0;
  49. while((c=bi.read())!=-1){
  50. response.getWriter().write(c);
  51. }
  52. response.getWriter().flush();
  53. fi.close();
  54. }
  55. catch(Exceptione){
  56. //TODOAuto-generatedcatchblock
  57. e.printStackTrace();
  58. }
  59. return"success";
  60. }
  61. /**
  62. *持久化到数据库
  63. *@return
  64. */
  65. publicStringinsertiImageInfo(){
  66. try{
  67. hotelimage=newHotelImage();
  68. hotelimage.setImageTitle(imageTitle);
  69. Stringimage=request.getParameter("uploadFileName");
  70. hotelimage.setImageName(request.getParameter("oldUploadFileName"));
  71. StringsavePath=request.getSession().getServletContext().getRealPath("/upload");
  72. try{
  73. hotelimage.setImagePath(URLEncoder.encode(image,"utf-8"));
  74. FileUtils.copyFile(newFile(savePath+"/temp/"+hotelimage.getImagePath()),newFile(savePath+"/hotel/"+hotelimage.getImagePath()));
  75. }catch(Exceptione){
  76. e.printStackTrace();
  77. }
  78. HttpSessionsession=request.getSession();
  79. //验证用户是否已登录
  80. if(session.getAttribute("AdminSession")!=null){
  81. UserInfouserInfo=(UserInfo)session.getAttribute("AdminSession");
  82. if(userInfo!=null){
  83. hotelimage.setUploadUserId(Integer.parseInt(userInfo.getId()));
  84. }
  85. }
  86. else{
  87. hotelimage.setUploadUserId(0);
  88. }
  89. hotelimage.setHotelId(hotelId);
  90. hotelimage.setImageType(imageType);
  91. hotelimage.setImageDesc(content);
  92. intt=hotelImageService.addHotelImageService(hotelimage);
  93. if(t<=0){
  94. return"error";
  95. }
  96. hotelId="0";
  97. imageType="0";
  98. imageTitle="";
  99. }
  100. catch(Exceptionex){
  101. ex.printStackTrace();
  102. return"error";
  103. }
  104. return"success";
  105. }
  106. publicStringgoEditImage(){
  107. Stringid=request.getParameter("id");
  108. HotelImageimage=hotelImageService.getImageByID(Long.valueOf(id));
  109. List<HotelInfo>hotels=hotelImageService.queryHotelListService(hotelInfo);
  110. request.setAttribute("hotels",hotels);
  111. request.setAttribute("image",image);
  112. if("audit".equals(request.getParameter("type"))){
  113. return"audit";
  114. }
  115. return"success";
  116. }
  117. publicStringeditImage(){
  118. try{
  119. hotelimage=newHotelImage();
  120. hotelimage.setImageTitle(imageTitle);
  121. Stringimage=request.getParameter("uploadFileName");
  122. hotelimage.setImageName(request.getParameter("oldUploadFileName"));
  123. StringsavePath=request.getSession().getServletContext().getRealPath("/upload");
  124. try{
  125. hotelimage.setImagePath(URLEncoder.encode(image,"utf-8"));
  126. if(!"0".equals(request.getParameter("upload"))){
  127. FileUtils.copyFile(newFile(savePath+"/temp/"+hotelimage.getImagePath()),newFile(savePath+"/hotel/"+hotelimage.getImagePath()));
  128. }
  129. }catch(Exceptione){
  130. e.printStackTrace();
  131. }
  132. HttpSessionsession=request.getSession();
  133. //验证用户是否已登录
  134. if(session.getAttribute("AdminSession")!=null){
  135. UserInfouserInfo=(UserInfo)session.getAttribute("AdminSession");
  136. if(userInfo!=null){
  137. hotelimage.setUploadUserId(Integer.parseInt(userInfo.getId()));
  138. }
  139. }
  140. else{
  141. hotelimage.setUploadUserId(0);
  142. }
  143. hotelimage.setHotelId(hotelId);
  144. hotelimage.setImageType(imageType);
  145. hotelimage.setImageDesc(content);
  146. hotelimage.setImageId(imageId);
  147. intt=hotelImageService.updateImage(hotelimage);
  148. if(t<=0){
  149. return"error";
  150. }
  151. hotelId="0";
  152. imageType="0";
  153. imageTitle="";
  154. }
  155. catch(Exceptionex){
  156. ex.printStackTrace();
  157. return"error";
  158. }
  159. returnActionSupport.SUCCESS;
  160. }
  161. publicStringauditImage(){
  162. HttpServletRequestrequest=ServletActionContext.getRequest();
  163. HttpSessionsession=request.getSession();
  164. UserInfouserInfo=(UserInfo)session.getAttribute("AdminSession");
  165. HotelImageimage=newHotelImage();
  166. image.setImageId(imageId);
  167. image.setAudit_flag(request.getParameter("audit_flag"));
  168. image.setAudit_fault_reson(request.getParameter("audit_fault_reason"));
  169. image.setAudit_user(Integer.valueOf(userInfo.getId()));
  170. hotelImageService.auditImage(image);
  171. returnActionSupport.SUCCESS;
  172. }
  173. publicStringdeleteImage(){
  174. HttpServletRequestrequest=ServletActionContext.getRequest();
  175. String[]ids=request.getParameterValues("checkedIds");
  176. StringnewsIds=StringUtils.join(ids,",");
  177. hotelImageService.deleteImageByIds(newsIds);
  178. return"success";
  179. }
  180. publicStringgetGallerialImage(){
  181. HotelImageimage=newHotelImage();
  182. image.setStartRow(1);
  183. image.setPageSize(20);
  184. Stringpath=request.getContextPath()+"/upload/hotel/";
  185. List<HotelImage>list=hotelImageService.getImageList(image);
  186. List<GalleriaImage>galleriaImages=newArrayList<GalleriaImage>();
  187. GalleriaImagegalleriaImage=null;
  188. for(inti=0;i<list.size();i++){
  189. galleriaImage=newGalleriaImage();
  190. image=list.get(i);
  191. galleriaImage.setDescription(image.getImageDesc());
  192. galleriaImage.setImage(path+image.getImagePath());
  193. galleriaImage.setTitle(image.getImageTitle());
  194. galleriaImages.add(galleriaImage);
  195. }
  196. JSONArrayjo=JSONArray.fromObject(galleriaImages);
  197. json=jo.toString();
  198. return"success";
  199. }
  200. }
  201. //gettersetter略
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics