文件上传服务端示例代码

移动端请求

  1. summer.upload({
  2. fileURL : src, //需要上传的文件路径
  3. type : "image/jpeg", //上传文件的类型 例:图片为"image/jpeg"
  4. params : {}, //上传参数
  5. SERVER : "http://ip:port/test/upload/";//服务器地址
  6. }, function(ret){
  7. alert("上传成功" + JSON.stringify(ret));
  8. } , function(err){
  9. alert("失败" + JSON.stingify(err));
  10. } );

文件上传服务端示例代码

将以下代码,导出jar包,放到webapp下
github下载

java示例代码

  1. package com.yonyou.example;
  2. import com.yonyou.uap.um.context.util.UmContextUtil;
  3. import com.yonyou.uap.um.controller.UmControllerException;
  4. import com.yonyou.uap.um.json.JSONTool;
  5. import java.io.File;
  6. import java.io.IOException;
  7. import java.io.InputStream;
  8. import java.io.OutputStream;
  9. import java.util.HashMap;
  10. import java.util.Iterator;
  11. import java.util.List;
  12. import java.util.Map;
  13. import javax.servlet.ServletContext;
  14. import javax.servlet.ServletException;
  15. import javax.servlet.http.HttpServlet;
  16. import javax.servlet.http.HttpServletRequest;
  17. import javax.servlet.http.HttpServletResponse;
  18. import nc.vo.jcom.lang.StringUtil;
  19. import org.apache.commons.fileupload.FileItem;
  20. import org.apache.commons.fileupload.disk.DiskFileItemFactory;
  21. import org.apache.commons.fileupload.servlet.ServletFileUpload;
  22. public class UploadServlet extends HttpServlet
  23. {
  24. private static final long serialVersionUID = 5009692626436714447L;
  25. protected void doGet(HttpServletRequest req, HttpServletResponse resp)
  26. throws ServletException, IOException
  27. {
  28. doPost(req, resp);
  29. }
  30. protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
  31. {
  32. String user = UmContextUtil.handleNULLStr(req.getParameter("user"));
  33. user = new String(user.getBytes("ISO-8859-1"), "utf-8");
  34. String appid = UmContextUtil.handleNULLStr(req.getParameter("appid"));
  35. appid = new String(appid.getBytes("ISO-8859-1"), "utf-8");
  36. String token = UmContextUtil.handleNULLStr(req.getParameter("token"));
  37. appid = new String(token.getBytes("ISO-8859-1"), "utf-8");
  38. String devid = UmContextUtil.handleNULLStr(req.getParameter("devid"));
  39. appid = new String(devid.getBytes("ISO-8859-1"), "utf-8");
  40. InputStream is = req.getInputStream();
  41. String retStr = "{"code":"0","msg":"false"}";
  42. if ((req.getContentLength() > 0) && (req.getContentType().contains("multipart")) && (is != null))
  43. {
  44. DiskFileItemFactory factory = new DiskFileItemFactory();
  45. factory.setSizeThreshold(4194304);
  46. ServletFileUpload upload = new ServletFileUpload(factory);
  47. List fileList = null;
  48. try
  49. {
  50. fileList = upload.parseRequest(req);
  51. String realPath = req.getServletContext().getRealPath("UPLOAD_FILES/");
  52. Map filePathMap = uploadFile(realPath, user, appid, fileList);
  53. if ((filePathMap == null) || (filePathMap.isEmpty()))
  54. break label360;
  55. String contextPath = req.getServletContext().getContextPath();
  56. String urlpath = contextPath + "/UPLOAD_FILES/" + filePathMap.get("filename");
  57. StringBuffer url = req.getRequestURL();
  58. Map map = filePathMap;
  59. map.put("code", "1");
  60. map.put("msg", "true");
  61. map.put("urlpath", urlpath);
  62. retStr = JSONTool.mapToJson(map, 4);
  63. }
  64. catch (Exception e)
  65. {
  66. throw new UmControllerException(e);
  67. }
  68. }
  69. label360: String contentType = "application/json;charset=UTF-8";
  70. resp.setContentType(contentType);
  71. resp.setHeader("Pragma", "No-cache");
  72. byte[] b = retStr.toString().getBytes("UTF-8");
  73. OutputStream out = resp.getOutputStream();
  74. out.write(b);
  75. }
  76. public Map<String, Object> uploadFile(String dir, String user, String appid, List<FileItem> listFile)
  77. {
  78. Map result = new HashMap();
  79. if ((listFile == null) || (listFile.size() == 0))
  80. return null;
  81. try
  82. {
  83. FileItem item;
  84. for (Iterator localIterator = listFile.iterator(); localIterator.hasNext(); ) { item = (FileItem)localIterator.next();
  85. String name = item.getFieldName();
  86. if (!(item.isFormField()))
  87. {
  88. break label127;
  89. }
  90. String value = item.getString();
  91. if ((StringUtil.isEmpty(user)) && ("user".equals(name.trim()))) {
  92. user = value;
  93. break label127: } if ((!(StringUtil.isEmpty(appid))) || (!("appid".equals(name.trim())))) break label127;
  94. label127: appid = value;
  95. }
  96. for (localIterator = listFile.iterator(); localIterator.hasNext(); ) { item = (FileItem)localIterator.next();
  97. if (item.isFormField())
  98. break label216;
  99. String fileName = item.getName().trim();
  100. File saveFile = new File(dir, fileName);
  101. item.write(saveFile);
  102. label216: result.put("filename", fileName);
  103. }
  104. }
  105. catch (Exception e)
  106. {
  107. e.printStackTrace();
  108. }
  109. return result;
  110. }
  111. }
文档更新时间: 2018-01-16 10:09