Browse Source

[永久素材][修改]:上传图片

release
Stormeye Wu 7 years ago
parent
commit
7cc12f5bf6
5 changed files with 75 additions and 9 deletions
  1. +7
    -1
      mlWechatOpen/src/main/java/com/iformall/WechatOpenApplication.java
  2. +65
    -8
      mlWechatOpen/src/main/java/com/iformall/controller/WechatMediaController.java
  3. +1
    -0
      mlWechatOpen/src/main/resources/application-dev.yml
  4. +1
    -0
      mlWechatOpen/src/main/resources/application-prod.yml
  5. +1
    -0
      mlWechatOpen/src/main/resources/application-test.yml

+ 7
- 1
mlWechatOpen/src/main/java/com/iformall/WechatOpenApplication.java View File

@@ -31,6 +31,9 @@ public class WechatOpenApplication {
@Value("${fm.deploy}") @Value("${fm.deploy}")
private Integer fmDeploy; private Integer fmDeploy;


@Value("${fm.upload_dir}")
private String uploadDir;

@Bean @Bean
public boolean isFmException() { public boolean isFmException() {
return fmException; return fmException;
@@ -49,7 +52,10 @@ public class WechatOpenApplication {
@Bean @Bean
public Integer getFmDeploy() { return fmDeploy; } public Integer getFmDeploy() { return fmDeploy; }



@Bean
public String fmUploadDir() {
return uploadDir;
}


public static void main(String[] args) { public static void main(String[] args) {
SpringApplication.run(WechatOpenApplication.class, args); SpringApplication.run(WechatOpenApplication.class, args);


+ 65
- 8
mlWechatOpen/src/main/java/com/iformall/controller/WechatMediaController.java View File

@@ -1,5 +1,6 @@
package com.iformall.controller; package com.iformall.controller;


import com.iformall.common.ResultData;
import com.iformall.service.WxAuthorizerInfoService; import com.iformall.service.WxAuthorizerInfoService;
import com.iformall.service.wechat.FmOpenService; import com.iformall.service.wechat.FmOpenService;
import io.swagger.annotations.Api; import io.swagger.annotations.Api;
@@ -8,16 +9,21 @@ import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation; import io.swagger.annotations.ApiOperation;
import me.chanjar.weixin.common.error.WxErrorException; import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.mp.api.WxMpService; import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.bean.material.WxMediaImgUploadResult;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller; import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;


import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream; import java.io.InputStream;
import java.util.UUID;


/** /**
* Stormeye Wu * Stormeye Wu
@@ -25,9 +31,11 @@ import java.io.InputStream;
@Controller @Controller
@RequestMapping("/material") @RequestMapping("/material")
@Api(description = "微信第三方开放平台素材文件") @Api(description = "微信第三方开放平台素材文件")
public class WechatMediaController {
public class WechatMediaController extends BaseController {
private final Logger logger = LoggerFactory.getLogger(getClass()); private final Logger logger = LoggerFactory.getLogger(getClass());
@Autowired @Autowired
private String fmUploadDir;
@Autowired
private FmOpenService openService; private FmOpenService openService;


@Autowired @Autowired
@@ -39,12 +47,11 @@ public class WechatMediaController {
@ApiImplicitParam(name = "appId", value = "appId", dataType = "String", paramType = "query", required = true), @ApiImplicitParam(name = "appId", value = "appId", dataType = "String", paramType = "query", required = true),
@ApiImplicitParam(name = "mediaId", value = "mediaId", dataType = "String", paramType = "query", required = true)}) @ApiImplicitParam(name = "mediaId", value = "mediaId", dataType = "String", paramType = "query", required = true)})
public void getMediaInfo(String appId, String mediaId, HttpServletRequest req, HttpServletResponse response) { public void getMediaInfo(String appId, String mediaId, HttpServletRequest req, HttpServletResponse response) {

InputStream inputStream = null; InputStream inputStream = null;
String filename = mediaId; String filename = mediaId;
try { try {
WxMpService maService = openService.getWxOpenComponentService().getWxMpServiceByAppid(appId);
inputStream = maService.getMaterialService().materialImageOrVoiceDownload(mediaId);
WxMpService mpService = openService.getWxOpenComponentService().getWxMpServiceByAppid(appId);
inputStream = mpService.getMaterialService().materialImageOrVoiceDownload(mediaId);
} catch (WxErrorException e) { } catch (WxErrorException e) {
logger.error("获取永久素材失败", e); logger.error("获取永久素材失败", e);
throw new RuntimeException(e); throw new RuntimeException(e);
@@ -57,11 +64,11 @@ public class WechatMediaController {
if (agent.contains("Firefox")) { if (agent.contains("Firefox")) {
response.setHeader("Content-disposition", response.setHeader("Content-disposition",
"attachment; filename=" "attachment; filename="
+ new String(filename.getBytes("GB2312"),"ISO-8859-1"));
+ new String(filename.getBytes("GB2312"), "ISO-8859-1"));
} else { } else {
response.setHeader("Content-disposition", response.setHeader("Content-disposition",
"attachment; filename=" "attachment; filename="
+ java.net.URLEncoder.encode(filename,"UTF-8"));
+ java.net.URLEncoder.encode(filename, "UTF-8"));
} }
// 循环取出流中的数据 // 循环取出流中的数据
byte[] b = new byte[1024]; byte[] b = new byte[1024];
@@ -73,4 +80,54 @@ public class WechatMediaController {
logger.error(e.getMessage()); logger.error(e.getMessage());
} }
} }

/**
* 图片上传
*
* @param multiReq
* @return
* @throws Exception
*/
@PostMapping(value = "/imgUpload/{appId}", consumes = "multipart/*", headers = "content-type=multipart/form-data")
@ApiOperation("上传图片")
public ResultData imgUpload(
@PathVariable("appId") String appId,
@RequestParam("file") MultipartFile multiReq) throws Exception {
logger.info("[" + getIpAddr() + "] WechatMediaController::imgUpload");

String fileName = multiReq.getOriginalFilename();
System.out.println(fileName);

// 本地保存
FileOutputStream fos = null;
String localImgFileName = fmUploadDir + fileName;
BufferedInputStream fs = null;
File localFile = new File(localImgFileName);
fos = new FileOutputStream(localFile);
fs = (BufferedInputStream) multiReq.getInputStream();
byte[] buffer = new byte[1024];
int len = 0;
while ((len = fs.read(buffer)) != -1) {
fos.write(buffer, 0, len);
}
fos.close();
fs.close();

// 上传 微信
try {
WxMpService mpService = openService.getWxOpenComponentService().getWxMpServiceByAppid(appId);
WxMediaImgUploadResult openResult = mpService.getMaterialService().mediaImgUpload(localFile);
if(openResult != null) {
return new ResultData(openResult.getUrl());
}
} catch (WxErrorException e) {
logger.error("获取永久素材失败", e);
throw new RuntimeException(e);
}

// 删除本地缓存
localFile.delete();

return new ResultData();
}
} }

+ 1
- 0
mlWechatOpen/src/main/resources/application-dev.yml View File

@@ -90,6 +90,7 @@ fm:
exception_emails: hupeng@iformall.com,wuguoqiang@iformall.com,gongbiao@iformall.com,luozukai@iformall.com exception_emails: hupeng@iformall.com,wuguoqiang@iformall.com,gongbiao@iformall.com,luozukai@iformall.com
audit_emails: wangmei@iformall.com,wuguoqiang@iformall.com,huanghui@iformall.com,sunmingming@iformall.com audit_emails: wangmei@iformall.com,wuguoqiang@iformall.com,huanghui@iformall.com,sunmingming@iformall.com
deploy: 1 deploy: 1
upload_dir: /home/test/server/uploads/


logging: logging:
level: level:


+ 1
- 0
mlWechatOpen/src/main/resources/application-prod.yml View File

@@ -84,6 +84,7 @@ fm:
exception_emails: hupeng@iformall.com,wuguoqiang@iformall.com,gongbiao@iformall.com,luozukai@iformall.com,hanxueda@iformall.com exception_emails: hupeng@iformall.com,wuguoqiang@iformall.com,gongbiao@iformall.com,luozukai@iformall.com,hanxueda@iformall.com
audit_emails: wangmei@iformall.com,wuguoqiang@iformall.com,huanghui@iformall.com,sunmingming@iformall.com audit_emails: wangmei@iformall.com,wuguoqiang@iformall.com,huanghui@iformall.com,sunmingming@iformall.com
deploy: 3 deploy: 3
upload_dir: /home/ec2-user/server/uploads/


logging: logging:
level: level:


+ 1
- 0
mlWechatOpen/src/main/resources/application-test.yml View File

@@ -94,6 +94,7 @@ fm:
exception_emails: hupeng@iformall.com,wuguoqiang@iformall.com,gongbiao@iformall.com,luozukai@iformall.com,hanxueda@iformall.com exception_emails: hupeng@iformall.com,wuguoqiang@iformall.com,gongbiao@iformall.com,luozukai@iformall.com,hanxueda@iformall.com
audit_emails: wangmei@iformall.com,wuguoqiang@iformall.com,huanghui@iformall.com,sunmingming@iformall.com audit_emails: wangmei@iformall.com,wuguoqiang@iformall.com,huanghui@iformall.com,sunmingming@iformall.com
deploy: 2 deploy: 2
upload_dir: /home/ec2-user/server/uploads/


ueditor: ueditor:
config: config.json config: config.json


Loading…
Cancel
Save