文件下载服务端代码示例

  1. package com.yonyou.uap.um.servlet;
  2. import java.io.File;
  3. import java.io.IOException;
  4. import java.io.OutputStream;
  5. import java.util.Map;
  6. import javax.servlet.ServletException;
  7. import javax.servlet.http.HttpServlet;
  8. import javax.servlet.http.HttpServletRequest;
  9. import javax.servlet.http.HttpServletResponse;
  10. import com.yonyou.uap.um.context.util.UmContextUtil;
  11. import com.yonyou.uap.um.context.util.UmContextUtil.FsOperator;
  12. import com.yonyou.uap.um.filestore.UmFileStoreException;
  13. import com.yonyou.uap.ump.log.UmpLogger;
  14. import com.yonyou.uap.ump.util.MAHomeFactory;
  15. public class DownloadServlet extends HttpServlet {
  16. private static final long serialVersionUID = 5009692626436714447L;
  17. @Override
  18. protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
  19. this.doPost(req, resp);
  20. }
  21. protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
  22. String user = UmContextUtil.handleNULLStr(req.getParameter(UmContextUtil.KEY_USER));
  23. user = new String(user.getBytes("ISO-8859-1"), "UTF-8");
  24. String appid = UmContextUtil.handleNULLStr(req.getParameter(UmContextUtil.KEY_APPID));
  25. appid = new String(appid.getBytes("ISO-8859-1"), "UTF-8");
  26. String token = UmContextUtil.handleNULLStr(req.getParameter(UmContextUtil.KEY_TOKEN));
  27. token = new String(token.getBytes("ISO-8859-1"), "UTF-8");
  28. String devid = UmContextUtil.handleNULLStr(req.getParameter(UmContextUtil.KEY_DEVID));
  29. devid = new String(devid.getBytes("ISO-8859-1"), "UTF-8");
  30. String fileName = UmContextUtil.handleNULLStr(req.getParameter(UmContextUtil.KEY_FILENAME));
  31. fileName = java.net.URLDecoder.decode(fileName, "ISO-8859-1");
  32. fileName = new String(fileName.getBytes("ISO-8859-1"), "UTF-8");
  33. if (fileName != null && !"".equalsIgnoreCase(fileName)) {
  34. String maHome = MAHomeFactory.MA_HOME;
  35. String fileDir = maHome + File.separator + "UPLOAD";
  36. try {
  37. downloadFile(fileDir, user, appid, resp, fileName);
  38. } catch (Exception e) {
  39. UmpLogger.error(e.getMessage());
  40. }
  41. }
  42. }
  43. /**
  44. * download file for download servlet
  45. *
  46. * @param dir
  47. * @param user
  48. * @param appid
  49. * @param response
  50. * @param fileName
  51. * @throws Exception
  52. */
  53. public static Map<String, Object> downloadFile(String dir, String user, String appid, HttpServletResponse response, String fileName) throws Exception {
  54. if (fileName == null || "".equalsIgnoreCase(fileName)) {
  55. return null;
  56. }
  57. OutputStream output = null;
  58. try {
  59. response.setContentType("application/x-download");
  60. response.addHeader("Content-Disposition", "attachment;filename=" + new String(fileName.getBytes("UTF-8"), "ISO8859_1"));
  61. response.setCharacterEncoding("UTF-8");
  62. // 获得输出流
  63. output = response.getOutputStream();
  64. // 写入文件流
  65. //...
  66. Map map = new HashMap();
  67. map.put("appid", appid);
  68. map.put("filename", filename);
  69. map.put("params", params);
  70. map.put("hostURI", getHostUri());
  71. byte[] bytes = ((ByteArrayOutputStream) output).toByteArray();
  72. byte[] bese64Bytes = Base64.encodeBase64(bytes);
  73. map.put(MAUtil.UPLOAD_KEY_FIELDS, new String(bese64Bytes));
  74. return map;
  75. } catch (Exception e) {
  76. throw new UmFileStoreException("um download file exception", e);
  77. } finally {
  78. try {
  79. if (output != null) {
  80. output.close();
  81. output = null;
  82. }
  83. } catch (Exception e) {
  84. throw new UmFileStoreException( "um file stream close exception", e);
  85. }
  86. }
  87. }
  88. }
文档更新时间: 2018-01-16 10:09