微信小程序上传图片后台java

时间:2024-10-03 15:11:11
/*微信小程序上传图片测试*/ @RequestMapping("wx_upload.do") public void wx_upload(HttpServletRequest request, HttpServletResponse response) throws Exception { ("utf-8"); //设置编码 //获得磁盘文件条目工厂 DiskFileItemFactory factory = new DiskFileItemFactory(); ServletFileUpload upload = new ServletFileUpload(factory); String fileId = null; String json = "{\"success\":false,\"fileName\":\"" + fileId + "\"}"; String pathUrl = FSDefaultMgr.E_DEFAULT.getDefaultUploadPathUrl();//获取图片服务器路径 InputStream inStream = null; try { //可以上传多个文件 List<FileItem> list = (List<FileItem>)(request); for(FileItem item : list){ //获取表单的属性名字 String name = (); //如果获取的 表单信息是普通的 文本 信息 if(()){ //获取用户具体输入的字符串 ,名字起得挺好,因为表单提交过来的是 字符串类型的 String value = () ; (name, value); }else { //获取路径名 String filename = (); (name, filename); inStream = () ; } } ByteArrayOutputStream swapStream = new ByteArrayOutputStream(); byte[] buff = new byte[100]; int rc = 0; while ((rc = (buff, 0, 100)) > 0) { (buff, 0, rc); } byte[] bytes = (); fileId = FastDFSClient.uploadFile(bytes, "", null); if (fileId != null) { json = "{\"success\":true,\"pathUrl\":\"" + pathUrl + "\",\"fileName\":\"" + fileId + "\"}"; } ().write(json); ().flush(); ().close(); }catch (Exception e) { (); } }