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)文章希望大家以后多多支持腳本之家!
- Java中的BaseTypeHandler自定義類型轉(zhuǎn)換器的使用
- Java基本數(shù)據(jù)類型之間的相互轉(zhuǎn)換詳解
- java中的類型自動(dòng)轉(zhuǎn)換機(jī)制解析
- Java之int和string類型轉(zhuǎn)換詳解
- java 實(shí)現(xiàn)將Object類型轉(zhuǎn)換為int類型
- Java中Object轉(zhuǎn)換為L(zhǎng)ist類型的實(shí)現(xiàn)方法
- Java將Date日期類型字段轉(zhuǎn)換成json字符串的方法
- Java數(shù)據(jù)類型分類與基本數(shù)據(jù)類型轉(zhuǎn)換
相關(guān)文章
如何使用Jenkins構(gòu)建GIT+Maven項(xiàng)目
這篇文章主要介紹了如何使用Jenkins構(gòu)建GIT+Maven項(xiàng)目,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-09-09
springboot實(shí)現(xiàn)瀏覽器截屏并添加文字
大家好,本篇文章主要講的是springboot實(shí)現(xiàn)瀏覽器截屏并添加文字,感興趣的同學(xué)趕快來(lái)看一看吧,對(duì)你有幫助的話記得收藏一下2022-02-02
Spring Boot與RabbitMQ結(jié)合實(shí)現(xiàn)延遲隊(duì)列的示例
本篇文章主要介紹了Spring Boot與RabbitMQ結(jié)合實(shí)現(xiàn)延遲隊(duì)列的示例,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-11-11
java利用DFA算法實(shí)現(xiàn)敏感詞過(guò)濾功能
在最近的開發(fā)中遇到了敏感詞過(guò)濾,便去網(wǎng)上查閱了很多敏感詞過(guò)濾的資料,在這里也和大家分享一下自己的理解。下面這篇文章主要給大家介紹了關(guān)于java利用DFA算法實(shí)現(xiàn)敏感詞過(guò)濾功能的相關(guān)資料,需要的朋友可以參考借鑒,下面來(lái)一起看看吧。2017-06-06
JAVA實(shí)現(xiàn)讀取txt文件內(nèi)容的方法
本篇文章主要介紹了JAVA實(shí)現(xiàn)讀取txt文件內(nèi)容的方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-01-01

