文件上传服务端示例代码
移动端请求
summer.upload({fileURL : src, //需要上传的文件路径type : "image/jpeg", //上传文件的类型 例:图片为"image/jpeg"params : {}, //上传参数SERVER : "http://ip:port/test/upload/";//服务器地址}, function(ret){alert("上传成功" + JSON.stringify(ret));} , function(err){alert("失败" + JSON.stingify(err));} );
文件上传服务端示例代码
将以下代码,导出jar包,放到webapp下
github下载
java示例代码
package com.yonyou.example;import com.yonyou.uap.um.context.util.UmContextUtil;import com.yonyou.uap.um.controller.UmControllerException;import com.yonyou.uap.um.json.JSONTool;import java.io.File;import java.io.IOException;import java.io.InputStream;import java.io.OutputStream;import java.util.HashMap;import java.util.Iterator;import java.util.List;import java.util.Map;import javax.servlet.ServletContext;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import nc.vo.jcom.lang.StringUtil;import org.apache.commons.fileupload.FileItem;import org.apache.commons.fileupload.disk.DiskFileItemFactory;import org.apache.commons.fileupload.servlet.ServletFileUpload;public class UploadServlet extends HttpServlet{private static final long serialVersionUID = 5009692626436714447L;protected void doGet(HttpServletRequest req, HttpServletResponse resp)throws ServletException, IOException{doPost(req, resp);}protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{String user = UmContextUtil.handleNULLStr(req.getParameter("user"));user = new String(user.getBytes("ISO-8859-1"), "utf-8");String appid = UmContextUtil.handleNULLStr(req.getParameter("appid"));appid = new String(appid.getBytes("ISO-8859-1"), "utf-8");String token = UmContextUtil.handleNULLStr(req.getParameter("token"));appid = new String(token.getBytes("ISO-8859-1"), "utf-8");String devid = UmContextUtil.handleNULLStr(req.getParameter("devid"));appid = new String(devid.getBytes("ISO-8859-1"), "utf-8");InputStream is = req.getInputStream();String retStr = "{"code":"0","msg":"false"}";if ((req.getContentLength() > 0) && (req.getContentType().contains("multipart")) && (is != null)){DiskFileItemFactory factory = new DiskFileItemFactory();factory.setSizeThreshold(4194304);ServletFileUpload upload = new ServletFileUpload(factory);List fileList = null;try{fileList = upload.parseRequest(req);String realPath = req.getServletContext().getRealPath("UPLOAD_FILES/");Map filePathMap = uploadFile(realPath, user, appid, fileList);if ((filePathMap == null) || (filePathMap.isEmpty()))break label360;String contextPath = req.getServletContext().getContextPath();String urlpath = contextPath + "/UPLOAD_FILES/" + filePathMap.get("filename");StringBuffer url = req.getRequestURL();Map map = filePathMap;map.put("code", "1");map.put("msg", "true");map.put("urlpath", urlpath);retStr = JSONTool.mapToJson(map, 4);}catch (Exception e){throw new UmControllerException(e);}}label360: String contentType = "application/json;charset=UTF-8";resp.setContentType(contentType);resp.setHeader("Pragma", "No-cache");byte[] b = retStr.toString().getBytes("UTF-8");OutputStream out = resp.getOutputStream();out.write(b);}public Map<String, Object> uploadFile(String dir, String user, String appid, List<FileItem> listFile){Map result = new HashMap();if ((listFile == null) || (listFile.size() == 0))return null;try{FileItem item;for (Iterator localIterator = listFile.iterator(); localIterator.hasNext(); ) { item = (FileItem)localIterator.next();String name = item.getFieldName();if (!(item.isFormField())){break label127;}String value = item.getString();if ((StringUtil.isEmpty(user)) && ("user".equals(name.trim()))) {user = value;break label127: } if ((!(StringUtil.isEmpty(appid))) || (!("appid".equals(name.trim())))) break label127;label127: appid = value;}for (localIterator = listFile.iterator(); localIterator.hasNext(); ) { item = (FileItem)localIterator.next();if (item.isFormField())break label216;String fileName = item.getName().trim();File saveFile = new File(dir, fileName);item.write(saveFile);label216: result.put("filename", fileName);}}catch (Exception e){e.printStackTrace();}return result;}}
文档更新时间: 2018-01-16 10:09