后端直接从文件URL下载文件
问题描述:
需要直接在后端根据文件url下载文件
解决方案:
可根据实际情况改代码:
public AidingStudentsImportExcelRespVO excelResolver(String url, Long aidingStudentsManageId){//创建temp.xlsxString 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.txtfos.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<AidingStudentsImportExcelVO> 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.txtfos.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定义urlString 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 blocke.printStackTrace();}} }
参考博客:
java 实现从url路径中下载文件到本地_con.setrequestproperty user-agent-CSDN博客
IO流【7】--- 通过IO流实现网络资源下载,通过URL地址下载图片等_后端 url地址转换为io流-CSDN博客