后端直接从文件URL下载文件
问题描述:
需要直接在后端根据文件url下载文件
解决方案:
可根据实际情况改代码:
public AidingStudentsImportExcelRespVO excelResolver(String url, Long aidingStudentsManageId){ //创建temp.xlsx String path = this.getClass().getResource("").getPath();//注意getResource("")里面是空字符串 String path1=path+"importexcel"; try { File dir = new File(path1); if(!dir.exists()){ dir.mkdirs();//创建目录 } File f = new File(path1,"temp.xlsx"); f.deleteOnExit(); if(!f.exists()){ f.createNewFile(); } } catch (Exception e) { e.printStackTrace(); } //从url读取出文件到已创建的文件中 URL website = null; ReadableByteChannel rbc = null; FileOutputStream fos = null; try { website = new URL(url); rbc = Channels.newChannel(website.openStream()); fos = new FileOutputStream(path1+"/temp.xlsx");//本地要存储的文件地址 例如:test.txt fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE); } catch (Exception e) { e.printStackTrace(); }finally{ if(fos!=null){ try { fos.close(); } catch (IOException e) { e.printStackTrace(); } } if(rbc!=null){ try { rbc.close(); } catch (IOException e) { e.printStackTrace(); } } } //导入名单 File file = new File(path1+"/temp.xlsx"); AidingStudentsImportExcelRespVO aidingStudentsImportExcelRespVO=null; try { MultipartFile cMultiFile = new MockMultipartFile("file", file.getName(), null, new FileInputStream(file)); List list = ExcelUtils.read(cMultiFile, AidingStudentsImportExcelVO.class); aidingStudentsImportExcelRespVO = aidingStudentsImportListService.aidingStudentsImportExcelListFromCloud(aidingStudentsManageId, list); } catch (IOException e) { throw exception(AIDING_STUDENTS_IMPORT_LIST_FILE_IMPORT_ERROR); } // 删除excel文件 file.deleteOnExit(); // 返回 return aidingStudentsImportExcelRespVO; }
关键部分(NIO):
} //从url读取出文件到已创建的文件中 URL website = null; ReadableByteChannel rbc = null; FileOutputStream fos = null; try { website = new URL(url); rbc = Channels.newChannel(website.openStream()); fos = new FileOutputStream(path1+"/temp.xlsx");//本地要存储的文件地址 例如:test.txt fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE); } catch (Exception e) { e.printStackTrace(); }finally{ if(fos!=null){ try { fos.close(); } catch (IOException e) { e.printStackTrace(); } } if(rbc!=null){ try { rbc.close(); } catch (IOException e) { e.printStackTrace();
另一种简单方法(IO):
import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.ByteArrayOutputStream; import java.io.FileOutputStream; import java.io.InputStream; import java.net.MalformedURLException; import java.net.URL; /** *从网络下载资源到本地 *步骤:1 定义URL,创建URL对象 2 打开URL对应的输入管道 3 使用输入管道读取URL的数据,使用内存流(ByteArrayOutputStream)接收URL的数据 4 网络URL的数据最终变成 byte[] 5 将内存流的数据写入到本地磁盘 */ public class DownResource { public static void main(String[] args) { //1定义url String urlPath = "http://172.16.59.99:9000/fdy-data/621b3868410512079f5501608a613466fb99c438b3e1583192a852c5b5e8f2b5.xlsx"; try { //2创建URL对象 URL url = new URL(urlPath); //打开URL对应的输入管道 try(InputStream in = url.openStream(); //内存流用来存储URL的数据 ByteArrayOutputStream out = new ByteArrayOutputStream(); //带有缓冲区的磁盘输出管道,唯一职责将内存流的字节数组写入到本地磁盘 BufferedOutputStream bos = new BufferedOutputStream( new FileOutputStream("logo.xlsx")) ){ byte [] buf= new byte[1024]; int length = 0; //使用URL的输入管道,读取URL的数据 while((length = in.read(buf))!=-1) { //内存流写入url读取的数据 out.write(buf,0,length); } out.flush(); //out.toByteArray()里面存放了百度LOGO的字节数组 //bos.write(out.toByteArray());内存流数据写入本地磁盘 bos.write(out.toByteArray()); bos.flush(); } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
参考博客:
java 实现从url路径中下载文件到本地_con.setrequestproperty user-agent-CSDN博客
IO流【7】--- 通过IO流实现网络资源下载,通过URL地址下载图片等_后端 url地址转换为io流-CSDN博客