Просмотр исходного кода

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

release
Stormeye Wu 7 лет назад
Родитель
Сommit
7cc12f5bf6
5 измененных файлов: 75 добавлений и 9 удалений
  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 Просмотреть файл

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

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

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


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

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


+ 65
- 8
mlWechatOpen/src/main/java/com/iformall/controller/WechatMediaController.java Просмотреть файл

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

import com.iformall.common.ResultData;
import com.iformall.service.WxAuthorizerInfoService;
import com.iformall.service.wechat.FmOpenService;
import io.swagger.annotations.Api;
@@ -8,16 +9,21 @@ import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.bean.material.WxMediaImgUploadResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
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.HttpServletResponse;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.UUID;

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

@Autowired
@@ -39,12 +47,11 @@ public class WechatMediaController {
@ApiImplicitParam(name = "appId", value = "appId", 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) {

InputStream inputStream = null;
String filename = mediaId;
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) {
logger.error("获取永久素材失败", e);
throw new RuntimeException(e);
@@ -57,11 +64,11 @@ public class WechatMediaController {
if (agent.contains("Firefox")) {
response.setHeader("Content-disposition",
"attachment; filename="
+ new String(filename.getBytes("GB2312"),"ISO-8859-1"));
+ new String(filename.getBytes("GB2312"), "ISO-8859-1"));
} else {
response.setHeader("Content-disposition",
"attachment; filename="
+ java.net.URLEncoder.encode(filename,"UTF-8"));
+ java.net.URLEncoder.encode(filename, "UTF-8"));
}
// 循环取出流中的数据
byte[] b = new byte[1024];
@@ -73,4 +80,54 @@ public class WechatMediaController {
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 Просмотреть файл

@@ -90,6 +90,7 @@ fm:
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
deploy: 1
upload_dir: /home/test/server/uploads/

logging:
level:


+ 1
- 0
mlWechatOpen/src/main/resources/application-prod.yml Просмотреть файл

@@ -84,6 +84,7 @@ fm:
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
deploy: 3
upload_dir: /home/ec2-user/server/uploads/

logging:
level:


+ 1
- 0
mlWechatOpen/src/main/resources/application-test.yml Просмотреть файл

@@ -94,6 +94,7 @@ fm:
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
deploy: 2
upload_dir: /home/ec2-user/server/uploads/

ueditor:
config: config.json


Загрузка…
Отмена
Сохранить