日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

图片上传至本地

發布時間:2023/12/10 编程问答 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 图片上传至本地 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

1.配置文件

bootstrap.yml file:path:httpResURL: http://122.246.1.148:19086SAVE_PATH: /data/imagesresURI: /wzq/

2.文件工具類

import cn.hutool.core.io.FileUtil; import com.vinsuan.park.platform.biz.FilePathConfig; import lombok.AllArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.web.multipart.MultipartFile;import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.time.LocalDateTime; import java.time.format.DateTimeFormatter;@Slf4j @AllArgsConstructor public class FileUtils {public static String upload(MultipartFile file, String pkno, String type, String plateNo) {LocalDateTime localDateTime = LocalDateTime.now();String yyyyMMdd = localDateTime.format(DateTimeFormatter.ofPattern("yyyyMMdd"));String HHmmss = localDateTime.format(DateTimeFormatter.ofPattern("HHmmss"));//文件名綁定后綴 pkno_type_plateNo_20191011_143600(車場-進出-車牌-年月日-時分秒)String md5File = pkno + "_" + type + "_" + plateNo + "_" + yyyyMMdd + "_" + HHmmss + ".jpg";File path = new File(FilePathConfig.savePath + FilePathConfig.resUri);if (!path.exists()) {path.mkdirs();}File localFile = new File(FilePathConfig.savePath + FilePathConfig.resUri, md5File);if (!FileUtil.exist(localFile)) {//創建一個新文件File attachFile = FileUtil.touch(FilePathConfig.savePath + FilePathConfig.resUri, md5File);//將文件流寫入文件中try {FileUtil.writeFromStream(file.getInputStream(), attachFile);} catch (IOException e) {e.printStackTrace();}}return FilePathConfig.httpResUrl + FilePathConfig.resUri + md5File;}/*** 保存文件(此案例中未使用)*/public static File saveFileFromInputStream(InputStream stream, String path, String filename, byte[] buff)throws IOException {File file = new File(path);if (!file.exists()) {log.info("++++++++++++++創建文件++++++++++++++++++++++++++++:" + path);file.mkdirs();}FileOutputStream fs = new FileOutputStream(path + filename);byte[] buffer = new byte[1024 * 1024];int byteread = 0;if (null == buff || buff.length <= 0) {while ((byteread = stream.read(buffer)) != -1) {fs.write(buffer, 0, byteread);}} else {fs.write(buff);}fs.flush();fs.close();if (null != stream) {stream.close();}return file;}// public static void main(String[] args) { // String imageStr = Base64Utils.GetImageStr("D:\\Pictures\\yy.jpg"); // MultipartFile file = Base64ToMultipartFile.base64ToMultipart(imageStr); // String upload = FileUtils.upload(file, "cc8888", "1", "浙B88888"); // System.out.println(upload); // } }

3.圖片轉圖片BASE64值
4.圖片BASE64轉MultipartFile
注意:
1.圖片存儲路徑為當前項目盤\data\images\wzq
2.此文件工具欄返回的圖片是路徑是:http://122.246.1.148:19999/wzq/20191022114711.jpg,不是:http://122.246.1.148:19999/data/images/20191022114711.jpg,是因為訪問路徑有做映射。wzq->data/images

總結

以上是生活随笔為你收集整理的图片上传至本地的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。