微服务zuul 上传图片 和 访问其他服务图片资源


1.zuul yml 配置:

spring:
application:
name: service-zuul
mvc:
static-path-pattern: /**
view:
prefix: ${server.servlet.context-path}
zuul:
routes:
api-a:
path: /webapi/**
serviceId: service-ribbon
2 ribbon yml 配置:
spring:
application:
name: Service-ribbon
mvc:
# 默认配置会自动在 /public /static /resources 目录寻找静态资源, 故不需要 /static 等前缀
static-path-pattern: /**

3.ribbon 上传图片

新增 Controller
import com.alibaba.fastjson.JSONObject;
import com.example.serviceribbon.model.check.MBackResult;
import com.example.serviceribbon.service.SUploadFile;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

@Scope("prototype")
@Controller
public class UploadFileController extends GeneralController {
@RequestMapping("/UploadFile")
@ResponseBody
public MBackResult SingleFileUpload(@RequestParam("file") MultipartFile[] file, @RequestParam("FileTypeName") String FileTypeName, @RequestParam("FTokenID") String FTokenID) {
JSONObject param = new JSONObject();
param.put("FTokenID", FTokenID);
if (CheckToken(param)) { //CheckToken(param)
SUploadFile sUploadFile = new SUploadFile();
this._BackResult.setFobject(sUploadFile.SingleFileUpload(file,FileTypeName));
}
return this._BackResult;
}

@RequestMapping("/UploadFileBase64")
@ResponseBody
public MBackResult SingleFileUploadImg(@RequestBody JSONObject param){
if (CheckToken(param)) {
SUploadFile sUploadFile = new SUploadFile();
this._BackResult.setFobject(sUploadFile.GenerateImage(
param.getString("ImgStr"),
param.getString("FileType")
)
);
}
return _BackResult;
}
}

新增 service
import com.example.serviceribbon.common.General;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.Base64;
import java.util.UUID;

public class SUploadFile {
/**
* 上传文件
*
* @param files
* @return
*/
public String SingleFileUpload(MultipartFile[] files,String FileTypeName) {
String FileName = "";
try {
String ditName="";
if(FileTypeName==null || FileTypeName.isEmpty()){
ditName="";
}else {
ditName=FileTypeName+"/";
}

for (MultipartFile file : files) {
String Filepath = General.Instance().getWebPath();
General.Instance().CreateDirectory(Filepath + "CreateFile/Image/"+ditName);
String fileTyle = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."), file.getOriginalFilename().length());
String ID = UUID.randomUUID().toString().replace("-", "");

Filepath = Filepath + "CreateFile/Image/"+ditName + ID + fileTyle;
FileName = "CreateFile/Image/"+ditName + ID + fileTyle;
// Get the file and save it somewhere
byte[] bytes = file.getBytes();
File fileSave = new File(Filepath);

if (!fileSave.exists()) {
fileSave.mkdirs();
} else {
fileSave.delete();
}

file.transferTo(fileSave);
General.Instance().Log("Error", java.util.logging.Level.WARNING,
Filepath);
//Path path = Paths.get(Filepath );

//Files.write(path, bytes);
}
return FileName;
} catch (Exception ex) {
General.Instance().Log("Error", java.util.logging.Level.WARNING,
"SUploadFile.SingleFileUpload:" + ex.getMessage());
return null;
} finally {

}


}

public String GenerateImage(String imgStr, String fileTyle) {// 对字节数组字符串进行Base64解码并生成图片
if (imgStr == null) // 图像数据为空
return "";
String[] arr= imgStr.split(",");
if(arr.length>1){
imgStr=arr[1];
}



try {
// Base64解码
byte[] bytes = Base64.getDecoder().decode(imgStr);
for (int i = 0; i < bytes.length; ++i) {
if (bytes[i] < 0) {// 调整异常数据
bytes[i] += 256;
}
}
String imgFilePath="";
String Filepath = General.Instance().getWebPath();
General.Instance().CreateDirectory(Filepath + "CreateFile/Image/");

String ID = UUID.randomUUID().toString().replace("-", "");

imgFilePath = Filepath + "CreateFile/Image/" + ID +"."+ fileTyle;
String FileName = "CreateFile/Image/" + ID +"."+ fileTyle;
// 生成jpeg图片
OutputStream out = new FileOutputStream(imgFilePath);
out.write(bytes);
out.flush();
out.close();
return FileName;
} catch (Exception e) {
return "";
}
}
}

4 访问图片 http://172.172.172.45:8769/webapi/CreateFile/Image/DeviceType/fc569ae4c6584ce9ad35b03da386ba66.png