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

歡迎訪問 生活随笔!

生活随笔

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

编程问答

java文件上传

發布時間:2025/4/16 编程问答 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java文件上传 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
public class ImageAnd64Binary {public static void main(String[] args){String imgSrcPath = "d:/abc/123.jpg"; //生成64編碼的圖片的路徑 String imgCreatePath = "E:\\apache-tomcat-6.0.37\\webapps/pro/ueditor2/jsp/upload1/20140318/480ace2bfc6e44608595bd4adbdeb067.jpg"; //64編碼生成圖片的路徑 imgCreatePath=imgCreatePath.replaceAll("\\\\", "/");System.out.println(imgCreatePath);String strImg = getImageStr(imgSrcPath);System.out.println(strImg);generateImage(strImg, imgCreatePath);}/** * 將圖片文件轉化為字節數組字符串,并對其進行Base64編碼處理 * @param imgSrcPath 生成64編碼的圖片的路徑 * @return */ public static String getImageStr(String imgSrcPath){InputStream in = null;byte[] data = null;//讀取圖片字節數組 try {in = new FileInputStream(imgSrcPath); data = new byte[in.available()];in.read(data);in.close();} catch (IOException e) {e.printStackTrace();}//對字節數組Base64編碼 BASE64Encoder encoder = new BASE64Encoder();return encoder.encode(data);//返回Base64編碼過的字節數組字符串 }/** * 對字節數組字符串進行Base64解碼并生成圖片 * @param imgStr 轉換為圖片的字符串 * @param imgCreatePath 64編碼生成圖片的路徑 * @return */ public static boolean generateImage(String imgStr, String imgCreatePath){if (imgStr == null) //圖像數據為空 return false; // 文件夾不存在,創建一個 File file = new File(imgCreatePath);File fileParent = file.getParentFile(); if(!fileParent.exists()){fileParent.mkdirs();}

BASE64Decoder decoder = new BASE64Decoder(); try { //Base64解碼 byte[] b = decoder.decodeBuffer(imgStr); for(int i=0;i<b.length;++i) { if(b[i]<0) {//調整異常數據 b[i]+=256; } } OutputStream out = new FileOutputStream(imgCreatePath); out.write(b); out.flush(); out.close(); return true; } catch (Exception e){ return false; } } }

總結

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

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