微信小程序的uploadFile

饿死的肉包 2017-02-24 03:29:05
在微信小程序使用uploadfile进行上传图片
代码如下:
wx.uploadFile({
url: "http://(接受请求的地址)/merchUpload",
filePath:tempFilePaths[0],//此处为图片的path
name:"file",
header: {
'content-type':'multipart/form-data'
}, // 设置请求的 header
formData: {
'user':'test',
}, // HTTP 请求中其他额外的 form data
success: function(res){
console.log("成功")
console.log(res);
},
fail: function(res) {
console.log("失败")
console.log(res);
}
})

后台java代码如下
public class AttachmentRest extends BaseRest{
private Logger logger = Logger.getLogger(this.getClass().getName());

@Resource(name="merchService")
private MerchService merchService;

@Path("merchUpload")
@POST
@Consumes("application/json")
@Produces("application/json")
public AttachmentDTO merchUpload(@Context HttpServletRequest request,
@Context HttpServletResponse response,AttachmentDTO attachmentDTO){
response.setCharacterEncoding("utf-8");
response.setHeader("Content-Encoding", "utf-8");
System.out.println("进来了");
// 记录日志
LogUtil.normalLog(logger, "version/version", "",
HttpUtil.getClientAddress(request), request.getHeader("User-agent"));
Attachment attachment = new Attachment();
String merchId = request.getParameter("merchId");
// 操作日志
try {
String currentDate = DateUtils.getCurrentDate("yyyyMMdd");
DiskFileItemFactory factory = new DiskFileItemFactory();
ResourceBundle systemConfig = ResourceBundle.getBundle(
"config/system", Locale.getDefault());
String uploadSysUrl = systemConfig.getString("merchEleSignUrl")+currentDate+"/";
String downloadSysUrl = systemConfig.getString("merchEleSignDownloadUrl")+currentDate+"/";
factory.setRepository(new File(uploadSysUrl));
// 设置 缓存的大小,当上传文件的容量超过该缓存时,直接放到 暂时存储室
factory.setSizeThreshold(1024 * 1024);
ServletFileUpload upload = new ServletFileUpload(factory);
ArrayList<FileItem> list = (ArrayList<FileItem>) upload.parseRequest(request);
for (FileItem item : list) {
String fileName = "";
// 获取表单的属性名字
String name = item.getFieldName();
System.out.println(name);
String fileNames = item.getName();
System.out.println(fileNames);
String path = uploadSysUrl + fileName;
String sqlPath = downloadSysUrl + fileName;
File saveFile = new File(path);
// 判断这个文件(saveFile)是否存在
if (!saveFile.getParentFile().exists()) {
// 如果不存在就创建这个文件夹
saveFile.getParentFile().mkdirs();
}
}

} catch(Exception e) {
LogUtil.error(logger, e);
attachmentDTO = new AttachmentDTO(ResultCode.Common.SYSTEM_ERROR_CODE,ResultCode.Common.SYSTEM_ERROR_DESC);
}
return attachmentDTO;
}
}

后台java接受提示
二月 24, 2017 3:24:19 下午 org.apache.cxf.jaxrs.utils.JAXRSUtils findTargetMethod
警告: .No operation matching request path /merchUpload is found, HTTP Method : POST, ContentType : multipart/form-data;boundary=--------------------------416512839884584675845067, Accept : */*,.
二月 24, 2017 3:24:19 下午 org.apache.cxf.jaxrs.impl.WebApplicationExceptionMapper toResponse
警告: WebApplicationException has been caught : no cause is available

完全搞不懂为嘛接收不到
...全文
8172 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
s_xice 2018-03-18
  • 打赏
  • 举报
回复
这里有一个小程序上传文件的完整示例,附源码 https://github.com/piscium2010/cos-wx-upload-file
饿死的肉包 2017-02-28
  • 打赏
  • 举报
回复
问题已经解决了 java后台代码有问题,之前是把请求的方式和返回结果的方式一起改变了,导致出了问题,希望大家以后都要细心一点 public class AttachmentRest extends BaseRest{ private Logger logger = Logger.getLogger(this.getClass().getName()); @Resource(name="merchService") private MerchService merchService; @Path("merchUpload") @POST @Consumes("multipart/form-data") /*****此处为接受请求的方式*****/ @Produces("application/json") /*****此处为返回结果的方式*****/ public AttachmentDTO merchUpload(@Context HttpServletRequest request, @Context HttpServletResponse response){ AttachmentDTO attachmentDTO = new AttachmentDTO(); response.setCharacterEncoding("utf-8"); response.setHeader("Content-Encoding", "utf-8");
饿死的肉包 2017-02-24
  • 打赏
  • 举报
回复
引用 1 楼 zpjshiwo77 的回复:
地址有问题,小程序只支持https协议~ 而且还要注意ssl证书的适用性问题,我在startSll申请的签发的证书在小程序编辑器里面没问题,在手机上测试的时候,微信浏览器不认这个机构签发的证书,所以也请求错误了~ (前面当我没说,我刚查API发现这个接口不需要https协议,ajax的请求才需要) 还有一个原因是你在小程序企业服务里面查找一下你的域名有没有加上审核,微信没审核通过的域名貌似也不能用
本地开发的话,我勾选了开发环境可以不校验域名,所以这样的请求时没问题的,而且后台能接到请求,并打印错误的日志,但是数据却接不到
zpjshiwo77 2017-02-24
  • 打赏
  • 举报
回复
地址有问题,小程序只支持https协议~ 而且还要注意ssl证书的适用性问题,我在startSll申请的签发的证书在小程序编辑器里面没问题,在手机上测试的时候,微信浏览器不认这个机构签发的证书,所以也请求错误了~ (前面当我没说,我刚查API发现这个接口不需要https协议,ajax的请求才需要) 还有一个原因是你在小程序企业服务里面查找一下你的域名有没有加上审核,微信没审核通过的域名貌似也不能用

5,658

社区成员

发帖
与我相关
我的任务
社区描述
Web开发应用服务器相关讨论专区
社区管理员
  • 应用服务器社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧