支付宝小程序uploadfile的坑

版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/Zhooson/article/details/90513697

代码是微信复制过来的,微信俩中写法都是可以

   header: {
                'Content-Type': 'application/x-www-form-urlencoded',
                // 'content-Type': 'application/json',
    },
 

支付宝只能写 ‘Content-Type’: ‘application/x-www-form-urlencoded’,

my.uploadFile({
  url: '请使用自己服务器地址',
  fileType: 'image',
  fileName: 'file',
  filePath: '...',
     header: {
                'Content-Type': 'application/x-www-form-urlencoded',
    },
  success: (res) => {
    my.alert({
	  content: '上传成功'
	});
  },
});
展开阅读全文

微信小程序uploadFile

02-24

在微信小程序使用uploadfile进行上传图片rn代码如下:rnwx.uploadFile(rn url: "http://(接受请求的地址)/merchUpload",rn filePath:tempFilePaths[0],//此处为图片的pathrn name:"file",rn header: rn 'content-type':'multipart/form-data'rn , // 设置请求的 headerrn formData: rn 'user':'test',rn , // HTTP 请求中其他额外的 form datarn success: function(res)rn console.log("成功")rn console.log(res);rn ,rn fail: function(res) rn console.log("失败")rn console.log(res);rn rn )rnrn后台java代码如下rnpublic class AttachmentRest extends BaseRestrn private Logger logger = Logger.getLogger(this.getClass().getName());rnrn @Resource(name="merchService")rn private MerchService merchService;rnrn @Path("merchUpload")rn @POSTrn @Consumes("application/json")rn @Produces("application/json")rn public AttachmentDTO merchUpload(@Context HttpServletRequest request,rn @Context HttpServletResponse response,AttachmentDTO attachmentDTO)rn response.setCharacterEncoding("utf-8");rn response.setHeader("Content-Encoding", "utf-8");rn System.out.println("进来了");rn // 记录日志rn LogUtil.normalLog(logger, "version/version", "", rn HttpUtil.getClientAddress(request), request.getHeader("User-agent")); rn Attachment attachment = new Attachment();rn String merchId = request.getParameter("merchId");rn // 操作日志rn try rn String currentDate = DateUtils.getCurrentDate("yyyyMMdd");rn DiskFileItemFactory factory = new DiskFileItemFactory();rn ResourceBundle systemConfig = ResourceBundle.getBundle(rn "config/system", Locale.getDefault());rn String uploadSysUrl = systemConfig.getString("merchEleSignUrl")+currentDate+"/";rn String downloadSysUrl = systemConfig.getString("merchEleSignDownloadUrl")+currentDate+"/";rn factory.setRepository(new File(uploadSysUrl));rn // 设置 缓存的大小,当上传文件的容量超过该缓存时,直接放到 暂时存储室rn factory.setSizeThreshold(1024 * 1024);rn ServletFileUpload upload = new ServletFileUpload(factory);rn ArrayList list = (ArrayList) upload.parseRequest(request);rn for (FileItem item : list) rn String fileName = "";rn // 获取表单的属性名字rn String name = item.getFieldName();rn System.out.println(name);rn String fileNames = item.getName();rn System.out.println(fileNames);rn String path = uploadSysUrl + fileName;rn String sqlPath = downloadSysUrl + fileName;rn File saveFile = new File(path);rn // 判断这个文件(saveFile)是否存在rn if (!saveFile.getParentFile().exists()) rn // 如果不存在就创建这个文件夹rn saveFile.getParentFile().mkdirs();rn rn rn rn catch(Exception e) rn LogUtil.error(logger, e);rn attachmentDTO = new AttachmentDTO(ResultCode.Common.SYSTEM_ERROR_CODE,ResultCode.Common.SYSTEM_ERROR_DESC);rn rn return attachmentDTO;rn rnrnrn后台java接受提示rn二月 24, 2017 3:24:19 下午 org.apache.cxf.jaxrs.utils.JAXRSUtils findTargetMethodrn警告: .No operation matching request path /merchUpload is found, HTTP Method : POST, ContentType : multipart/form-data;boundary=--------------------------416512839884584675845067, Accept : */*,.rn二月 24, 2017 3:24:19 下午 org.apache.cxf.jaxrs.impl.WebApplicationExceptionMapper toResponsern警告: WebApplicationException has been caught : no cause is availablernrn完全搞不懂为嘛接收不到 论坛

没有更多推荐了,返回首页