用Java到指定地址下载东西放到指定文件夹下

1.获取指定地址的输入流.URL url = new URL("url");InputStream in = url.openStream();2.在指定文件夹下创建文件。File dir = new File("指定文件夹");File file = new File(dir,"文件名");3.将下载保存到文件。FileOutputStream out = new FileOutputStream(file);Streams.copy(in, out, true);

您可以使用Java的IO流来下载图片并保存到指定文件夹中。以下是一个简单的示例代码: ```java import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.FileOutputStream; import java.io.IOException; import java.net.URL; import java.net.URLConnection; public class ImageDownloader { public static void downloadImage(String imageUrl, String savePath) throws IOException { URL url = new URL(imageUrl); URLConnection conn = url.openConnection(); conn.setConnectTimeout(5000); conn.setReadTimeout(5000); BufferedInputStream in = new BufferedInputStream(conn.getInputStream()); BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(savePath)); byte[] buf = new byte[1024]; int len = 0; while ((len = in.read(buf)) != -1) { out.write(buf, 0, len); } out.close(); in.close(); } public static void main(String[] args) throws IOException { String imageUrl = "http://example.com/image.jpg"; String savePath = "/path/to/save/image.jpg"; downloadImage(imageUrl, savePath); } } ``` 您可以将上述代码中的`imageUrl`替换为您要下载的图片的URL,将`savePath`替换为您要保存图片的文件路径。如果您需要将多个图片压缩成一个压缩包,可以使用Java的ZipOutputStream类来实现。以下是示例代码: ```java import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.net.URL; import java.net.URLConnection; import java.util.zip.ZipEntry; import java.util.zip.ZipOutputStream; public class ImageDownloader { public static void downloadImage(String imageUrl, String savePath) throws IOException { URL url = new URL(imageUrl); URLConnection conn = url.openConnection(); conn.setConnectTimeout(5000); conn.setReadTimeout(5000); BufferedInputStream in = new BufferedInputStream(conn.getInputStream()); BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(savePath)); byte[] buf = new byte[1024]; int len = 0; while ((len = in.read(buf)) != -1) { out.write(buf, 0, len); } out.close(); in.close(); } public static void compressImagesToZip(String[] imageUrls, String zipFilePath) throws IOException { ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(zipFilePath))); byte[] buf = new byte[1024]; for (int i = 0; i < imageUrls.length; i++) { String imageUrl = imageUrls[i]; String fileName = "image" + i + ".jpg"; ZipEntry entry = new ZipEntry(fileName); out.putNextEntry(entry); BufferedInputStream in = new BufferedInputStream(new URL(imageUrl).openStream()); int len; while ((len = in.read(buf)) > 0) { out.write(buf, 0, len); } in.close(); out.closeEntry(); } out.close(); } public static void main(String[] args) throws IOException { String[] imageUrls = {"http://example.com/image1.jpg", "http://example.com/image2.jpg"}; String zipFilePath = "/path/to/save/images.zip"; compressImagesToZip(imageUrls, zipFilePath); } } ``` 您可以将上述代码中的`imageUrls`替换为您要下载并压缩的图片的URL数组,将`zipFilePath`替换为您要保存压缩包的文件路径。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值