加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

flex与java集成下载功能实现

发布时间:2020-12-15 01:00:24 所属栏目:百科 来源:网络整理
导读:?适用于附件放在数据库中的下载方式 ?flex端: navigateToURL(new?URLRequest( "URL.do" +id),? "_blank" ); --发送的请求路径 java端: ? package?com.zjht.hrm.action;? ? import?java.io.BufferedOutputStream;? import?java.io.IOException;? import?java

?适用于附件放在数据库中的下载方式

?flex端:

 
 
  1. navigateToURL(new?URLRequest("URL.do"+id),?"_blank"); --发送的请求路径

java端:

?

 
 
  1. package?com.zjht.hrm.action;?
  2. ?
  3. import?java.io.BufferedOutputStream;?
  4. import?java.io.IOException;?
  5. import?java.io.OutputStream;?
  6. import?java.io.PrintWriter;?
  7. import?org.apache.commons.codec.binary.*;?
  8. import?javax.servlet.http.HttpServletRequest;?
  9. import?javax.servlet.http.HttpServletResponse;?
  10. ?
  11. import?nl.bitwalker.useragentutils.Browser;?
  12. import?nl.bitwalker.useragentutils.UserAgent;?
  13. ?
  14. import?org.springframework.stereotype.Controller;?
  15. import?org.springframework.web.bind.annotation.RequestMapping;?
  16. import?org.springframework.web.bind.annotation.RequestMethod;?
  17. ?
  18. import?com.zjht.hrm.dao.AttachmentDao;?
  19. import?com.zjht.hrm.model.Attachment;?
  20. ?
  21. @Controller?
  22. public?class?AttachmentDownloadAction?{?
  23. ?
  24. ????private?AttachmentDao?attachmentDao;?
  25. ?
  26. ????@RequestMapping(value?=?"AttachmentDownload",?method?=?RequestMethod.GET)?
  27. ????public?void?attachmentDownloadById(final?long?id,?final?boolean?fail,?
  28. ????????????final?HttpServletRequest?request,?final?HttpServletResponse?response)?{?
  29. ????????Attachment?entity?=?null;?
  30. ????????try?{?
  31. ????????????entity?=?attachmentDao.findById(id);?
  32. ????????????if(entity==null){?
  33. ????????????????try?{?
  34. ????????????????????response.sendError(HttpServletResponse.SC_FORBIDDEN);?
  35. ????????????????????return;?
  36. ????????????????}?catch?(IOException?e)?{?
  37. ????????????????}?
  38. ????????????}?
  39. ????????????byte[]?data?=?entity.getContent();?
  40. ????????????String?fileName?=?entity.getFileName()?==?null???""?:?entity?
  41. ????????????????????.getFileName();?
  42. ????????????if?(isImage(entity.getFileType())?&&?!fail)?{?
  43. ????????????????//imageDownload(request,?response,?entity);?
  44. ????????????????PrintWriter?out?=?response.getWriter();?
  45. ????????????????out.println("<img?src="AttachmentDownload.do?id="+entity.getId()?
  46. ????????????????????????+"&fail=true"?/>");?
  47. ????????????????out.flush();?
  48. ????????????????out.close();?
  49. ????????????????return;?
  50. ????????????}?
  51. ????????????response.reset();?
  52. ????????????response.setStatus(200);?
  53. ????????????FileType?fileType=getFileType(entity.getFileType());?
  54. ????????????UserAgent?userAgent?=?UserAgent.parseUserAgentString(request?
  55. ????????????????????.getHeader("user-agent"));?
  56. ????????????if?(userAgent.getBrowser().getGroup()?==?Browser.IE)?
  57. ????????????????fileName=java.net.URLEncoder.encode(fileName,"UTF-8");?
  58. ????????????else?
  59. ????????????????fileName?=?new?String(fileName.getBytes("UTF-8"),"ISO8859_1");?
  60. ????????????if(fileType==FileType.img?||?fileType==FileType.pdf)?
  61. ????????????{?
  62. ????????????????response.setHeader("Content-Disposition",?"inline;?filename=""?
  63. ????????????????????????+?fileName?+?""");?
  64. ????????????}else{?
  65. ????????????????response.setHeader("Content-Disposition",?"attachment;?filename=""?
  66. ????????????????????????+?fileName?+?""");?
  67. ????????????}?
  68. ????????????response.addHeader("Content-Length",?""?+?data.length);?
  69. ????????????response.setHeader("Connection",?"close");?
  70. ????????????response.setContentType(getContentType(entity.getFileType()));?
  71. ????????????OutputStream?outputStream?=?new?BufferedOutputStream(?
  72. ????????????????????response.getOutputStream());?
  73. ????????????outputStream.write(data);?
  74. ????????????outputStream.flush();?
  75. ????????????outputStream.close();?
  76. ????????}?catch?(Exception?e1)?{?
  77. ????????????try?{?
  78. ????????????????response.sendError(HttpServletResponse.SC_FORBIDDEN);?
  79. ????????????}?catch?(IOException?e)?{?
  80. ????????????}?
  81. ????????}?
  82. ?
  83. ????}?
  84. ?
  85. ????@SuppressWarnings("unused")?
  86. ????private?void?imageDownload(final?HttpServletRequest?request,?
  87. ????????????final?HttpServletResponse?response,?Attachment?entity)?
  88. ????????????throws?IOException?{?
  89. ????????PrintWriter?out?=?response.getWriter();?
  90. ????????UserAgent?userAgent?=?UserAgent.parseUserAgentString(request?
  91. ????????????????.getHeader("user-agent"));?
  92. ????????if?(userAgent.getBrowser()?==?Browser.IE5?
  93. ????????????????||?userAgent.getBrowser()?==?Browser.IE5_5?
  94. ????????????????||?userAgent.getBrowser()?==?Browser.IE6?
  95. ????????????????||?userAgent.getBrowser()?==?Browser.IE7)?{?
  96. ????????????out.println("<img?src="AttachmentDownload.do?id="+entity.getId()?
  97. ????????????????????+"&fail=true"?/>");?
  98. ?
  99. ????????}?else?{?
  100. ????????????String?content?=?Base64.encodeBase64String(entity.getContent());?
  101. ????????????out.println("<img?src="data:image/"?+?entity.getFileType()?
  102. ????????????????????+?";base64,"?+?content?+?""?/>");?
  103. ????????}?
  104. ????????out.flush();?
  105. ????????out.close();?
  106. ????}?
  107. ?
  108. ????public?void?setAttachmentDao(AttachmentDao?attachmentDao)?{?
  109. ????????this.attachmentDao?=?attachmentDao;?
  110. ????}?
  111. ?
  112. ????private?String?getContentType(String?extension)?{?
  113. ????????FileType?fileType?=?null;?
  114. ????????try?{?
  115. ????????????fileType?=?Enum.valueOf(FileType.class,?extension);?
  116. ????????}?catch?(Exception?e)?{?
  117. ????????????return?"application/octet-stream;charset=UTF-8";?
  118. ????????}?
  119. ????????switch?(fileType)?{?
  120. ????????case?xls:?
  121. ????????????//?contentType设定?
  122. ????????????return?"application/vnd.ms-excel;charset=utf-8";?
  123. ????????case?xlsx:?
  124. ????????????//?contentType设定?
  125. ????????????return?"application/vnd.ms-excel;charset=utf-8";?
  126. ????????case?pdf:?
  127. ????????????//?contentType设定?
  128. ????????????return?"application/pdf;charset=utf-8";?
  129. ????????case?doc:?
  130. ????????????//?contentType设定?
  131. ????????????return?"application/msword;charset=utf-8";?
  132. ????????case?docx:?
  133. ????????????//?contentType设定?
  134. ????????????return?"application/msword;charset=utf-8";?
  135. ????????case?txt:?
  136. ????????????//?contentType设定?
  137. ????????????return?"text/plain;charset=utf-8";?
  138. ????????default:?
  139. ????????????//?contentType设定?
  140. ????????????return?"application/octet-stream;charset=UTF-8";?
  141. ????????}?
  142. ????}?
  143. ?
  144. ????public?enum?FileType?{?
  145. ????????xls("xls"),?xlsx("xlsx"),?pdf("pdf"),?doc("doc"),?docx("docx"),?txt(?
  146. ????????????????"txt"),gif("gif"),jpeg("jpeg"),jpg("jpg"),png("png"),img("img");?
  147. ?
  148. ????????FileType(String?name)?{?
  149. ????????????this.name?=?name;?
  150. ????????}?
  151. ?
  152. ????????private?String?name;?
  153. ?
  154. ????????public?String?getName()?{?
  155. ????????????return?name;?
  156. ????????}?
  157. ?
  158. ????????public?void?setName(String?name)?{?
  159. ????????????this.name?=?name;?
  160. ????????}?
  161. ????}?
  162. ?????
  163. ????private?FileType?getFileType(String?extension){?
  164. ????????FileType?fileType?=?null;?
  165. ????????try?{?
  166. ????????????fileType?=?Enum.valueOf(FileType.class,?extension);?
  167. ????????}?catch?(Exception?e)?{?
  168. ????????????return?null;?
  169. ????????}?
  170. ????????switch?(fileType)?{?
  171. ????????case?pdf:?
  172. ????????????return?fileType;?
  173. ????????case?gif:?
  174. ????????????return?FileType.img;?
  175. ????????case?jpeg:?
  176. ????????????return?FileType.img;?
  177. ????????case?jpg:?
  178. ????????????return?FileType.img;?
  179. ????????case?png:?
  180. ????????????return?FileType.img;?
  181. ????????default:?
  182. ????????????return?null;?
  183. ????????}?
  184. ????}?
  185. ?????
  186. ?
  187. ????private?boolean?isImage(String?fileType)?{?
  188. ????????if?(fileType.equals("png")?||?fileType.equals("gif")?
  189. ????????????????||?fileType.equals("jpg"))?{?
  190. ????????????return?true;?
  191. ????????}?else?{?
  192. ????????????return?false;?
  193. ????????}?
  194. ????}?
  195. }?

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读