base64轉MultipartFile
文件上傳接口傳入字符串類型的參數後將參數轉爲MultipartFile格式,其中須要工具類(轉爲MultipartFile)
package com.social.util;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import org.springframework.web.multipart.MultipartFile;
import sun.misc.BASE64Decoder;
public class BASE64DecodedMultipartFile implements MultipartFile {
private final byte[] imgContent;
private final String header;
public BASE64DecodedMultipartFile(byte[] imgContent, String header) {
this.imgContent = imgContent;
this.header = header.split(";")[0];
}
@Override
public String getName() {
// TODO - implementation depends on your requirements
return System.currentTimeMillis() + Math.random() + "." + header.split("/")[1];
}
@Override
public String getOriginalFilename() {
// TODO - implementation depends on your requirements
return System.currentTimeMillis() + (int)Math.random() * 10000 + "." + header.split("/")[1];
}
@Override
public String getContentType() {
// TODO - implementation depends on your requirements
return header.split(":")[1];
}
@Override
public boolean isEmpty() {
return imgContent == null || imgContent.length == 0;
}
@Override
public long getSize() {
return imgContent.length;
}
@Override
public byte[] getBytes() throws IOException {
return imgContent;
}
@Override
public InputStream getInputStream() throws IOException {
return new ByteArrayInputStream(imgContent);
}
@Override
public void transferTo(File dest) throws IOException, IllegalStateException {
new FileOutputStream(dest).write(imgContent);
}
public static MultipartFile base64ToMultipart(String base64) {
try {
String[] baseStrs = base64.split(",");
BASE64Decoder decoder = new BASE64Decoder();
byte[] b = new byte[0];
if(baseStrs.length>1){
b = decoder.decodeBuffer(baseStrs[1]);
for(int i = 0; i < b.length; ++i) {
if (b[i] < 0) {
b[i] += 256;
}
}
}
return new BASE64DecodedMultipartFile(b, baseStrs[0]);
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
}
而後將參數file以及request傳入service層,在service層寫業務邏輯,
如下是上傳圖片代碼
Map resultMap=new HashMap();
try {
if (request instanceof MultipartHttpServletRequest) {
uploadFile = ((MultipartHttpServletRequest)request).getFile("file");
}
if(uploadFile!=null && uploadFile.getOriginalFilename()
!=null && uploadFile.getOriginalFilename().length()>0){
/**
* 項目服務器地址路徑
*/
String projectServerPath = request.getScheme() + "://"+request.getServerName()+":" +
request.getServerPort() + request.getContextPath() + "/upload/";
/**
* 上傳文件絕對路徑
*/
String path = request.getSession().getServletContext().getRealPath("/upload/");
//原始名稱
String oldFileName = uploadFile.getOriginalFilename();
//新的圖片名稱
String newFileName = UUID.randomUUID() + oldFileName.substring(oldFileName.lastIndexOf("."));
//新圖片
File newFile = new File(path + "\\" + newFileName);
/**
* 判斷路徑是否存在,若是不存在就建立一個
*/
if (!newFile.getParentFile().exists()) {
newFile.getParentFile().mkdirs();
}
/**
* 建立文件
*/
uploadFile.transferTo(new File(path + File.separator + newFileName));
String serverFilePatn = projectServerPath + newFileName;
resultMap.put("error", 0);
resultMap.put("url",serverFilePatn);
return WxResult.ok(resultMap);
}else{
//返回結果
resultMap.put("error", 1);
resultMap.put("message", "文件異常");
return WxResult.ok(resultMap);
}
} catch (Exception e) {
resultMap.put("error", 1);
resultMap.put("message", "文件上傳發生異常");
return WxResult.ok(resultMap);
}
複製代碼