微信小程序的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
完全搞不懂为嘛接收不到