//前端圖片是Base64字符串形式傳遞圖片參數;需要用Base解密,寫入到本地磁盤中
public String upload(String string){
解析圖片(Base64):
response.setHeader("Access-Control-Allow-Origin","*"); // 第二個參數填寫允許跨域的域名稱,不建議直接寫 "*"
response.setHeader("Access-Control-Allow-Headers", "X-Requested-With");
response.setHeader("Access-Control-Allow-Methods", "PUT,POST,GET,DELETE,OPTIONS");
// 接收跨域的cookie
response.setHeader("Access-Control-Allow-Credentials", "true");
url = url.substring(url.indexOf(",")+1,url.length());
String imagePath = null;
BASE64Decoder decoder = new BASE64Decoder();
String strName = null;
String string2 = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+request.getContextPath();
System.out.println(string2);
try {
byte[] bytes = decoder.decodeBuffer(url);
// 處理數據
for (int i = 0; i < bytes.length; ++i) {
if (bytes[i] < 0) {
bytes[i] += 256;
}
}
strName = UUID.randomUUID().toString();
System.out.println(strName);
imagePath = "E:/javaworkspace/project/rbApi/image/"+strName+".png";
OutputStream out = new FileOutputStream(imagePath);
out.write(bytes);
out.flush();
out.close();
}catch (Exception e){
e.printStackTrace();
}
}
//springboot項目
//以@RequestParam("url") List<MultipartFile> url接收上傳圖片;寫入到本地磁盤中
//返回路徑是磁盤路徑,將磁盤路徑映射到外部靜態資源;訪問:項目路徑+映射路徑+文件名
@RequestMapping(value = "/uploadImage")
@ResponseBody
public PictureUrl upload(@RequestParam("url") List<MultipartFile> url, HttpServletRequest request, HttpServletResponse response){
PictureUrl pictureUrl = new PictureUrl();
String contentPath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+request.getContextPath();
try{
if(url.size() == 0){
pictureUrl.setStatusText("false");
}else{
OutputStream os = null;
List<String> urlList = new ArrayList<>();
for (MultipartFile file : url){
String fileName = file.getOriginalFilename();
String fileNam2 = UUID.randomUUID().toString();
String imagePath = "E:/javaworkspace/project/rbApi/image/"+fileNam2+".png";
File file1 = new File(imagePath);
os = new FileOutputStream(file1);
os.write(file.getBytes());
//String imagePath2 = contentPath + "/showImage?id=" + fileNam2+".png";
//String imagePath2 = upload + fileNam2+".png";
String imagePath2 = contentPath+"/image/"+fileNam2+".png";
urlList.add(imagePath2);
System.out.println("文件路徑:"+imagePath2);
}
pictureUrl.setStatusText("ok");
pictureUrl.setImageUrl(urlList);
os.flush();
os.close();
}
}catch (Exception e){
e.printStackTrace();
pictureUrl.setStatusText("false");
return pictureUrl;
}
return pictureUrl;
}
//將圖片以二進制的形式輸出,前端路徑可為:(項目路徑+方法+文件名)
/**
* 回顯圖片
* @param id
* @param response
* @return
*/
@RequestMapping("/showImage")
@ResponseBody
public AjaxResult download(String id, HttpServletResponse response){
try{
response.setContentType("image/jpeg/jpg/png/gif/bmp/tiff/svg");
String path ="E:/javaworkspace/project/rbApi/image/"+id;
File file = new File(path);//括號里參數為文件圖片路徑
if(file.exists()){ //如果文件存在
InputStream in = new FileInputStream(path); //用該文件創建一個輸入流
OutputStream os = response.getOutputStream(); //創建輸出流
byte[] b = new byte[1024];
while( in.read(b)!= -1){
os.write(b);
}
in.close();
os.flush();
os.close();
}
return null;
}catch (Exception e){
e.printStackTrace();
return AjaxResult.build("false",null );
}
}
//將磁盤文件路徑映射為項目訪問路徑
@Configuration
public class WebMvcConfiguration implements WebMvcConfigurer {
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/image/**").addResourceLocations("file:E:/javaworkspace/project/rbApi/image/");
}
}
得到