1. 程式人生 > >java的檔案上傳工具類

java的檔案上傳工具類

package util; import java.awt.image.BufferedImage; import java.io.ByteArrayInputStream; import java.io.File; import java.io.IOException; import java.io.InputStream; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; import javax.imageio.ImageIO; import org.apache.commons.io.FileUtils; import org.springframework.web.multipart.MultipartFile; import sun.misc.BASE64Decoder; /**  * 上傳檔案  *   */ public class FileUpload { /** * @param file *            //檔案物件 * @param filePath *            //上傳路徑 * @param fileName *            //檔名 * @return 檔名 */ public static String fileUp(MultipartFile file, String filePath, String fileName) { String extName = ""; // 副檔名格式: File file2 = new File(filePath); // 如果資料夾不存在則建立 if (!file2.exists() && !file2.isDirectory()) { System.out.println(filePath + "//不存在"); file2.mkdir(); } try { if (file.getOriginalFilename().lastIndexOf(".") >= 0) { extName = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf(".")); } copyFile(file.getInputStream(), filePath, fileName + extName).replaceAll("-", ""); } catch (IOException e) { System.out.println(e); } return fileName + extName; } public static String fileUpV2(String file, String filePath, String fileName) { String extName = ".png"; // 副檔名格式: try { BASE64Decoder decoder = new BASE64Decoder(); // Base64解碼 byte[] b = decoder.decodeBuffer(file); for (int i = 0; i < b.length; ++i) { if (b[i] < 0) {// 調整異常資料 b[i] += 256; } } InputStream inputStream = FileUpload.byte2Input(b); copyFile(inputStream, filePath, fileName + extName).replaceAll("-", ""); } catch (IOException e) { System.out.println(e); } return fileName + extName; } public static InputStream fileUpV3(String file) { try { BASE64Decoder decoder = new BASE64Decoder(); // Base64解碼 byte[] b = decoder.decodeBuffer(file); for (int i = 0; i < b.length; ++i) { if (b[i] < 0) {// 調整異常資料 b[i] += 256; } } InputStream inputStream = new ByteArrayInputStream(b); return inputStream; } catch (IOException e) { System.out.println(e); } return null; } /** * 處理webp processWebp 處理webp (這裡描述這個方法適用條件 – 可選) 標記:@param inputStream 標記:@param * file2 標記:@return 返回值:File 作者:brandon *  * @exception @since *                1.0.0 */ public static File processWebp(InputStream inputStream, String file2) { try { BufferedImage im = ImageIO.read(inputStream); File file = new File(file2); if (!file.exists()) { if (!file.getParentFile().exists()) { file.getParentFile().mkdirs(); } file.createNewFile(); } ImageIO.write(im, "webp", file); return file; } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return null; } /** *  * processPng(這裡用一句話描述這個方法的作用) (這裡描述這個方法適用條件 – 可選) 標記:@param inputStream * 標記:@param file2 標記:@return 返回值:File 作者:brandon *  * @exception @since *                1.0.0 */ public static File processPng(InputStream inputStream, String file2) { try { BufferedImage im = ImageIO.read(inputStream); File file = new File(file2); if (!file.exists()) { if (!file.getParentFile().exists()) { file.getParentFile().mkdirs(); } file.createNewFile(); } ImageIO.write(im, "png", file); return file; } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return null; } /** *  * processPng(這裡用一句話描述這個方法的作用) (這裡描述這個方法適用條件 – 可選) 標記:@param im 標記:@param file2 * 標記:@return 返回值:File 作者:brandon *  * @exception @since *                1.0.0 */ public static File processPng(BufferedImage im, String file2) { try { File file = new File(file2); if (!file.exists()) { if (!file.getParentFile().exists()) { file.getParentFile().mkdirs(); } file.createNewFile(); } ImageIO.write(im, "png", file); return file; } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return null; } /** *  * byte2Input(這裡用一句話描述這個方法的作用) (這裡描述這個方法適用條件 – 可選) 標記:@param buf 標記:@return * 返回值:InputStream 作者:brandon *  * @exception @since *                1.0.0 */ public static final InputStream byte2Input(byte[] buf) { return new ByteArrayInputStream(buf); } /** * 寫檔案到當前目錄的upload目錄中 *  * @param in * @param fileName * @throws IOException */ private static String copyFile(InputStream in, String dir, String realName) throws IOException { File file = new File(dir, realName); System.out.println("copy file before================= " + dir); if (!file.exists()) { System.out.println("copy file path not exist================= " + dir); if (!file.getParentFile().exists()) { file.getParentFile().mkdirs(); } file.createNewFile(); } FileUtils.copyInputStreamToFile(in, file); return realName; } public static InputStream returnBitMap(String path) { URL url = null; InputStream is = null; try { url = new URL(path); } catch (MalformedURLException e) { e.printStackTrace(); } try { HttpURLConnection conn = (HttpURLConnection) url.openConnection();// 利用HttpURLConnection物件,我們可以從網路中獲取網頁資料. conn.setDoInput(true); conn.connect(); is = conn.getInputStream(); // 得到網路返回的輸入流 } catch (IOException e) { e.printStackTrace(); } return is; } /** *  * fileUpReturnFile(這裡用一句話描述這個方法的作用) (這裡描述這個方法適用條件 – 可選) 標記:@param file * 標記:@param filePath 標記:@param fileName 標記:@return 返回值:File 作者:brandon *  * @exception @since *                1.0.0 */ public static File fileUpReturnFile(MultipartFile file, String filePath, String fileName) { String extName = ""; // 副檔名格式: File result_file = null; try { if (file.getOriginalFilename().lastIndexOf(".") >= 0) { extName = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf(".")); } result_file = copyFileReturnFile(file.getInputStream(), filePath, fileName + extName); } catch (IOException e) { System.out.println(e); } return result_file; } /** *  * copyFileReturnFile(這裡用一句話描述這個方法的作用) (這裡描述這個方法適用條件 – 可選) 標記:@param in * 標記:@param dir 標記:@param realName 標記:@return 標記:@throws IOException 返回值:File * 作者:brandon *  * @exception @since *                1.0.0 */ private static File copyFileReturnFile(InputStream in, String dir, String realName) throws IOException { File file = new File(dir, realName); System.out.println("copy file before================= " + dir); if (!file.exists()) { System.out.println("copy file path not exist================= " + dir); if (!file.getParentFile().exists()) { file.getParentFile().mkdirs(); } file.createNewFile(); } FileUtils.copyInputStreamToFile(in, file); return file; } }