java中通过url相应文件夹_java 根据Url下载对应的文件到指定位置,读txt文件获取url...

package test;

import java.io.BufferedReader;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileOutputStream;

import java.io.InputStream;

import java.io.InputStreamReader;

import java.io.OutputStream;

import java.net.URL;

import java.net.URLConnection;

import java.util.ArrayList;

import java.util.List;

public class testFile {

//public static void main(String[] args) throws Exception {

// TODO Auto-generated method stub

// download("http://avatar.csdn.net/1/3/B/1_li1325169021.jpg", "1_li1325169021.jpg","d:\\image\\");

// }

//链接url下载图片

public static void download(String urlString, String filename,String savePath) throws Exception {

// 构造URL

URL url = new URL(urlString);

// 打开连接

URLConnection con = url.openConnection();

//设置请求超时为5s

con.setConnectTimeout(5*1000);

// 输入流

InputStream is = con.getInputStream();

// 1K的数据缓冲

byte[] bs = new byte[1024];

// 读取到的数据长度

int len;

// 输出的文件流

File sf=new File(savePath);

if(!sf.exists()){

sf.mkdirs();

}

OutputStream os = new FileOutputStream(sf.getPath()+"\\"+filename);

// 开始读取

while ((len = is.read(bs)) != -1) {

os.write(bs, 0, len);

}

// 完毕,关闭所有链接

os.close();

is.close();

}

public static void main(String[] args) {

try {

// 将D:/test.txt文件读取到输入流中

InputStream input = new FileInputStream("C:/Users/Admin/Desktop/路径.txt");

InputStream inputTwo = new FileInputStream("C:/Users/Admin/Desktop/文件名.txt");

// 创建BufferedReader,以gb2312的编码方式读取文件

BufferedReader reader = new BufferedReader(new InputStreamReader(input, "utf-8"));

BufferedReader readerTwo = new BufferedReader(new InputStreamReader(inputTwo, "utf-8"));

List fileUrl = new ArrayList();//图片路径List

List fileUrlTwo = new ArrayList();//图片名称List

String line = null;

String lineTwo = null;

// 按行读取文本,直到末尾(一般都这么写)

while ((line = reader.readLine()) != null) {

// 打印当前行字符串

fileUrl ff = new fileUrl();

ff.setPathUrl(line);

fileUrl.add(ff);

}

while ((lineTwo = readerTwo.readLine()) != null) {

// 打印当前行字符串

lineTwo = lineTwo.substring(lineTwo.lastIndexOf("/")+1,lineTwo.length());

fileUrlTwo fg = new fileUrlTwo();

fg.setFileName(lineTwo);

fileUrlTwo.add(fg);

}

String savePath = "D:\\text\\";//保存本地url

for(int i=0; i<246;i++){//下载条数,现给的死值,可根据list.size()获取

String fileurl = fileUrl.get(i).getPathUrl();//下载路径

String pathName = fileUrlTwo.get(i).getFileName();//图片名称

System.out.println("图片下载Url:"+fileurl+"图片保存对应名称:"+pathName);

download(fileurl,pathName,savePath);

}

} catch (Exception e) {

e.printStackTrace();

}

}

}

Java,要根据URL获取文件,并按文件夹进行分类并压缩打包,可以使用一些常见的类库和方法来实现。 首先,我们需要使用Java的网络编程来获取指定URL文件。可以使用URLURLConnection类来建立连接,并使用输入流将文件内容取到内存。如果需要处理HTTPS协议,还需要使用HttpsURLConnection类。 接下来,我们需要对获取文件进行按文件夹分类。可以通过解析URL的路径信息来提取文件夹路径,然后创建对应文件夹。可以使用java.nio.file包下的Path和Files类来处理文件路径和创建文件夹。 然后,我们需要将分类后的文件进行压缩打包。可以使用Java的压缩库,如java.util.zip包下的ZipOutputStream类来创建压缩文件,并使用FileOutputStream将压缩文件写入磁盘。可以先获取每个文件的输入流,然后使用ZipEntry将文件放入压缩文件。 最后,记得在完成操作后关闭打开的资源,如关闭输入流和输出流。 下面是一个简单的示例代码,演示如何根据URL获取文件,按文件夹分类并压缩打包: ```java import java.io.*; import java.net.URL; import java.nio.file.*; import java.util.zip.*; public class FileDownloader { public static void main(String[] args) { String urlPath = "http://example.com/path/to/files/"; String savePath = "C:/save/"; String zipFilePath = "C:/save/archive.zip"; try { // 创建连接 URL url = new URL(urlPath); InputStream inputStream = url.openStream(); BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream)); // 解析路径并创建文件夹 Path saveDir = Paths.get(savePath); Files.createDirectories(saveDir); // 创建压缩文件 FileOutputStream fos = new FileOutputStream(zipFilePath); ZipOutputStream zipOut = new ZipOutputStream(fos); // 文件并分类 String line; while ((line = reader.readLine()) != null) { // 解析文件名和路径 String fileName = line.substring(line.lastIndexOf("/") + 1); String folderPath = line.substring(urlPath.length(), line.lastIndexOf("/")); // 创建分类文件夹 Path folderDir = saveDir.resolve(folderPath); Files.createDirectories(folderDir); // 将文件放入压缩文件 ZipEntry zipEntry = new ZipEntry(folderPath + "/" + fileName); zipOut.putNextEntry(zipEntry); // 写入文件内容 byte[] buffer = new byte[4096]; int bytesRead; while ((bytesRead = inputStream.read(buffer)) != -1) { zipOut.write(buffer, 0, bytesRead); } } // 关闭资源 reader.close(); zipOut.close(); fos.close(); System.out.println("文件下载、分类和压缩打包完成!"); } catch (IOException e) { e.printStackTrace(); } } } ``` 请注意,上述代码只是简单示例,实际使用可能还需要进行错误处理、异常捕获和其他特殊需求的处理。同时,也可以根据具体情况进行优化和调整。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值