Commit 229b4b44 by danmo

素材上传问题处理

1 parent de038f66
...@@ -41,7 +41,7 @@ export default { ...@@ -41,7 +41,7 @@ export default {
this.loading = true this.loading = true
getDetail(id).then(({ data }) => { getDetail(id).then(({ data }) => {
this.form = data this.form = data
this.materialSelected = data.weMaterial.materialUrl this.materialSelected = data.weMaterial ==null?"":data.weMaterial.materialUrl
this.loading = false this.loading = false
}) })
}, },
......
...@@ -6,6 +6,7 @@ import com.dtflys.forest.annotation.Query; ...@@ -6,6 +6,7 @@ import com.dtflys.forest.annotation.Query;
import com.linkwechat.wecom.domain.dto.WeMediaDto; import com.linkwechat.wecom.domain.dto.WeMediaDto;
import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.MultipartFile;
import java.io.File;
import java.io.InputStream; import java.io.InputStream;
/** /**
...@@ -18,12 +19,12 @@ public interface WeMediaClient { ...@@ -18,12 +19,12 @@ public interface WeMediaClient {
/** /**
* 上传临时素材 * 上传临时素材
* @param multipartFile * @param file
* @param type * @param type
* @return * @return
*/ */
@Post(url="/media/upload") @Post(url="/media/upload")
WeMediaDto upload(@DataFile(value = "file") MultipartFile multipartFile, @Query("type") String type); WeMediaDto upload(@DataFile(value = "media") File file, @Query("type") String type);
/** /**
* 上传图片 * 上传图片
...@@ -40,6 +41,6 @@ public interface WeMediaClient { ...@@ -40,6 +41,6 @@ public interface WeMediaClient {
* 使用byte数组和Inputstream对象时一定要定义fileName属性 * 使用byte数组和Inputstream对象时一定要定义fileName属性
*/ */
@Post(url = "/media/upload") @Post(url = "/media/upload")
WeMediaDto upload(@DataFile(value = "file", fileName = "${1}") InputStream in, String filename,@Query("type") String type); WeMediaDto upload(@DataFile(value = "media", fileName = "${1}") InputStream file, String filename, @Query("type") String type);
} }
...@@ -181,7 +181,7 @@ public class WeEventChangeExternalContactImpl implements WeCallBackEventFactory ...@@ -181,7 +181,7 @@ public class WeEventChangeExternalContactImpl implements WeCallBackEventFactory
} }
if(StringUtils.isNotEmpty(messageMap.getCategoryId())){ if(StringUtils.isNotEmpty(messageMap.getCategoryId())){
WeMediaDto weMediaDto = weMaterialService WeMediaDto weMediaDto = weMaterialService
.uploadTemporaryMaterial(messageMap.getMaterialUrl(), MediaType.IMAGE.getMediaType()); .uploadTemporaryMaterial(messageMap.getMaterialUrl(),messageMap.getMaterialName(), MediaType.IMAGE.getMediaType());
Optional.ofNullable(weMediaDto).ifPresent(media ->{ Optional.ofNullable(weMediaDto).ifPresent(media ->{
weWelcomeMsgBuilder.image(WeWelcomeMsg.Image.builder().media_id(media.getMedia_id()) weWelcomeMsgBuilder.image(WeWelcomeMsg.Image.builder().media_id(media.getMedia_id())
.pic_url(media.getUrl()).build()); .pic_url(media.getUrl()).build());
......
...@@ -5,6 +5,7 @@ import com.dtflys.forest.exceptions.ForestRuntimeException; ...@@ -5,6 +5,7 @@ import com.dtflys.forest.exceptions.ForestRuntimeException;
import com.dtflys.forest.http.ForestRequest; import com.dtflys.forest.http.ForestRequest;
import com.dtflys.forest.http.ForestResponse; import com.dtflys.forest.http.ForestResponse;
import com.dtflys.forest.interceptor.Interceptor; import com.dtflys.forest.interceptor.Interceptor;
import com.dtflys.forest.multipart.ForestMultipart;
import com.dtflys.forest.utils.ForestDataType; import com.dtflys.forest.utils.ForestDataType;
import com.linkwechat.common.config.WeComeConfig; import com.linkwechat.common.config.WeComeConfig;
import com.linkwechat.common.constant.WeConstans; import com.linkwechat.common.constant.WeConstans;
...@@ -14,6 +15,7 @@ import org.springframework.beans.factory.annotation.Autowired; ...@@ -14,6 +15,7 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import java.util.Arrays; import java.util.Arrays;
import java.util.List;
/** /**
* @description: 微信token拦截器 * @description: 微信token拦截器
...@@ -38,16 +40,15 @@ public class WeAccessTokenInterceptor implements Interceptor{ ...@@ -38,16 +40,15 @@ public class WeAccessTokenInterceptor implements Interceptor{
*/ */
@Override @Override
public boolean beforeExecute(ForestRequest request) { public boolean beforeExecute(ForestRequest request) {
request.setDataType(ForestDataType.JSON);
String uri=request.getUrl().replace("http://",""); String uri=request.getUrl().replace("http://","");
//request.setContentType("application/json");
if(!Arrays.asList(weComeConfig.getFileUplodUrl()).contains(uri)){
request.setDataType(ForestDataType.JSON);
request.setContentType("application/json"); request.setContentType("application/json");
// if(Arrays.asList(weComeConfig.getFileUplodUrl()).contains(uri)){ }
// request.setContentType("application/json");
// }else{
// request.setContentType("multipart/form-data");
// }
......
...@@ -2,6 +2,7 @@ package com.linkwechat.wecom.service.impl; ...@@ -2,6 +2,7 @@ package com.linkwechat.wecom.service.impl;
import cn.hutool.core.collection.CollectionUtil; import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateUtil; import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.FileUtil;
import com.linkwechat.common.config.RuoYiConfig; import com.linkwechat.common.config.RuoYiConfig;
import com.linkwechat.common.config.ServerConfig; import com.linkwechat.common.config.ServerConfig;
import com.linkwechat.common.constant.WeConstans; import com.linkwechat.common.constant.WeConstans;
...@@ -17,9 +18,19 @@ import com.linkwechat.wecom.domain.vo.WeMaterialFileVO; ...@@ -17,9 +18,19 @@ import com.linkwechat.wecom.domain.vo.WeMaterialFileVO;
import com.linkwechat.wecom.mapper.WeMaterialMapper; import com.linkwechat.wecom.mapper.WeMaterialMapper;
import com.linkwechat.wecom.service.IWeMaterialService; import com.linkwechat.wecom.service.IWeMaterialService;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.common.error.WxError;
import me.chanjar.weixin.common.util.http.apache.Utf8ResponseHandler;
import org.apache.commons.fileupload.FileItem; import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileItemFactory; import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.disk.DiskFileItemFactory; import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.mime.HttpMultipartMode;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType; import org.springframework.http.MediaType;
import org.springframework.mock.web.MockMultipartFile; import org.springframework.mock.web.MockMultipartFile;
...@@ -135,32 +146,30 @@ public class WeMaterialServiceImpl implements IWeMaterialService { ...@@ -135,32 +146,30 @@ public class WeMaterialServiceImpl implements IWeMaterialService {
public WeMediaDto uploadTemporaryMaterial(String url, String type) { public WeMediaDto uploadTemporaryMaterial(String url, String type) {
try { try {
File file = new File(url); File file = new File(url);
FileItemFactory factory = new DiskFileItemFactory(16, null); return weMediaClient.upload(file, type);
FileItem item=factory.createItem(file.getName(),"text/plain",true,file.getName());
MultipartFile multipartFile = new CommonsMultipartFile(item);
return weMediaClient.upload(multipartFile, type);
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
log.error("上传临时文件失败......url:{},type:{},ex:{},st:{}",url,type,e.getMessage(),e.getStackTrace()); log.error("上传临时文件失败......url:{},type:{},ex:{},st:{}",url,type,e.getMessage(),e.getStackTrace());
} }
return null; return null;
} }
@Override @Override
public WeMediaDto uploadTemporaryMaterial(String url, String type, String name) { public WeMediaDto uploadTemporaryMaterial(String url, String type, String name) {
HttpURLConnection conn = null; HttpURLConnection conn = null;
try{ try{
URL materialUrl = new URL(url); URL materialUrl = new URL(url);
conn = (HttpURLConnection) materialUrl.openConnection(); conn = (HttpURLConnection) materialUrl.openConnection();
conn.setRequestMethod("GET"); conn.setRequestMethod("GET");
// conn.setConnectTimeout(20 * 1000); conn.setConnectTimeout(20 * 1000);
InputStream inputStream = conn.getInputStream(); return weMediaClient.upload(conn.getInputStream(), name, type);
return weMediaClient.upload(inputStream, name,type);
}catch (Exception e){ }catch (Exception e){
e.printStackTrace(); e.printStackTrace();
log.error("上传临时文件失败......url:{},type:{},name:{},ex:{},st:{}",url,type,name,e.getMessage(),e.getStackTrace()); log.error("上传临时文件失败......url:{},type:{},name:{},ex:{},st:{}",url,type,name,e.getMessage(),e.getStackTrace());
}finally {
if (conn !=null){
conn = null;
}
} }
return null; return null;
} }
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!