Java實(shí)現(xiàn)各種文件類型轉(zhuǎn)換方式(收藏)
1.網(wǎng)絡(luò)資源轉(zhuǎn)File
需要引入依賴commons-io
/** * 讀取網(wǎng)絡(luò)中的圖片 * @param url https://www.kziyue.com/wp-content/uploads/2019/06/5bca-hxyuaph9825616.jpg * @return */ public File URLToFile(String url){ File file1 = new File("test.png"); try { URL url1 = new URL(url); FileUtils.copyURLToFile(url1,file1); } catch (IOException e) { e.printStackTrace(); } File absoluteFile = file1.getAbsoluteFile(); return file1; }
2.網(wǎng)絡(luò)資源轉(zhuǎn)MultipartFile
需要引入依賴spring-web
/** * 文件上傳 * @param urlStr url地址 * @return multipartFile */ public MultipartFile fileUpload(String urlStr) throws Exception { try { //把地址轉(zhuǎn)換成URL對(duì)象 URL url = new URL(urlStr); //創(chuàng)建http鏈接 HttpURLConnection conn = (HttpURLConnection)url.openConnection(); //設(shè)置超時(shí)間為3秒 conn.setConnectTimeout(3*1000); //防止屏蔽程序抓取而返回403錯(cuò)誤 conn.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36)"); //得到輸入流 InputStream inputStream = conn.getInputStream(); //截取鏈接中的文件名 String fileName= urlStr.substring(urlStr.lastIndexOf("/")+1); MultipartFile multipartFile = new MockMultipartFile(fileName,fileName, ContentType.APPLICATION_OCTET_STREAM.toString(), inputStream); return multipartFile; } catch (Exception e) { e.printStackTrace(); } throw new Exception(); }
3.File轉(zhuǎn)MultipartFile
需要引用的依賴spring-text,httpcore
/** * 文件類型轉(zhuǎn)換 * * @param filePath 文件file * @return MultipartFile */ public static MultipartFile caseFileToMultipartFile(File filePath) { MultipartFile multipartFile = null; try { log.info("開始進(jìn)行文件轉(zhuǎn)換"); FileInputStream fileInputStream = new FileInputStream(filePath); multipartFile = new MockMultipartFile(filePath.getName(), filePath.getName(), ContentType.APPLICATION_OCTET_STREAM.toString(), fileInputStream); } catch (IOException e) { e.printStackTrace(); return null; } return multipartFile; }
4.File轉(zhuǎn)字節(jié)數(shù)組
/** * 將文件轉(zhuǎn)為字節(jié)數(shù)組 * @param file * @param size 1024 * @return */ public static byte[] BufferStreamForByte(File file, int size) { byte[] content = null; try { BufferedInputStream bis = null; ByteArrayOutputStream out = null; try { FileInputStream input = new FileInputStream(file); bis = new BufferedInputStream(input, size); byte[] bytes = new byte[1024]; int len; out = new ByteArrayOutputStream(); while ((len = bis.read(bytes)) > 0) { out.write(bytes, 0, len); } bis.close(); content = out.toByteArray(); } finally { if (bis != null) { bis.close(); } if (out != null) { out.close(); } } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return content; }
5.Frame轉(zhuǎn)BufferedImage
需要引入依賴javacv
public static BufferedImage FrameToBufferedImage(Frame frame) { //創(chuàng)建BufferedImage對(duì)象 Java2DFrameConverter converter = new Java2DFrameConverter(); BufferedImage bufferedImage = converter.getBufferedImage(frame); return bufferedImage; }
6.BufferedImage轉(zhuǎn)MultipartFile
public static MultipartFile fileCase(BufferedImage image){ //得到BufferedImage對(duì)象 // BufferedImage bufferedImage = JoinTwoImage.testEncode(200, 200, url); MultipartFile multipartFile= null; try { //創(chuàng)建一個(gè)ByteArrayOutputStream ByteArrayOutputStream os = new ByteArrayOutputStream(); //把BufferedImage寫入ByteArrayOutputStream ImageIO.write(image, "jpg", os); //ByteArrayOutputStream轉(zhuǎn)成InputStream InputStream input = new ByteArrayInputStream(os.toByteArray()); //InputStream轉(zhuǎn)成MultipartFile multipartFile =new MockMultipartFile("file", "file.jpg", "text/plain", input); } catch (IOException e) { e.printStackTrace(); } return multipartFile; }
到此這篇關(guān)于Java 各種文件類型轉(zhuǎn)換的方法的文章就介紹到這了,更多相關(guān)Java 文件類型轉(zhuǎn)換內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java打亂ArrayList生成一個(gè)隨機(jī)序列列表
有時(shí)候會(huì)需要將一個(gè)ArrayList或者數(shù)組中的數(shù)字打亂,方便后續(xù)使用,比如隨機(jī)出題、答案選項(xiàng)打亂、連線題打亂、抽獎(jiǎng)號(hào)碼打亂等等,把我自己寫的一段代碼貼出來分享給大家。2016-08-08Java實(shí)現(xiàn)FTP上傳到服務(wù)器
這篇文章主要為大家詳細(xì)介紹了Java實(shí)現(xiàn)FTP上傳到服務(wù)器,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-09-09Java使用FilenameFilter查找出目錄下指定后綴的文件示例
這篇文章主要介紹了Java使用FilenameFilter查找出目錄下指定后綴的文件,結(jié)合實(shí)例形式分析了java基于FilenameFilter類的文件遍歷、查找相關(guān)操作技巧,需要的朋友可以參考下2019-10-10SpringBoot實(shí)現(xiàn)設(shè)置全局和局部時(shí)間格式化
本文主要介紹了SpringBoot實(shí)現(xiàn)設(shè)置全局和局部時(shí)間格式化,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-01-01淺談SpringBoot中的Bean初始化方法?@PostConstruct
這篇文章主要介紹了SpringBoot中的Bean初始化方法?@PostConstruct,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-11-11如何使用SpringBootCondition更自由地定義條件化配置
這篇文章主要介紹了如何使用SpringBootCondition更自由地定義條件化配置,幫助大家更好的理解和學(xué)習(xí)使用springboot框架,感興趣的朋友可以了解下2021-04-04