图片上传功能思路:常见的上传有通过接收base64和接收multipart两种格式的格式,我这边用的是前段将图片装成base64传给我,我用读流方式将其上传到服务器某个路劲下,然后当前端需要这个图片是,我直接给他路径就可以,具体代码如下
/**
*
* @param path 保存路径
* @param fileName 文件名
* @param file 文件
* @param flag
*/
//图片上传base64格式
public void uploadImg(String path, String fileName, String file,String flag){
String base64Data = file.split(",")[1];
/**
* 2.解码成字节数组
*/
Base64.Decoder decoder = Base64.getDecoder();
byte[] bytes = decoder.decode(base64Data);
File file1 = new File(path);
if (!file1.exists()) {
file1.mkdir();
}
if (bytes.length>0) {
FileOutputStream ws = null;
try {
ws = new FileOutputStream(path+fileName);
ws.write(bytes);
ws.close();
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
}