介绍
场景:有一个图片的压缩包,里面的图片是按照系列存的。上传图片时需要按商品编号找到文件夹,进入文件夹选取里面的图片。所以写了下面的,按照文件名创建文件夹,然后把所有文件名的图片放进去,如果一个文件名有多个图片,则文件名按照后缀加-0,-1区别。例如,有一个商品图片,有3张,D304,分别存在3个文件夹中。经过处理后需要创建一个D304的文件夹,然后把3张图片存进去,名称分别为D304, D304-1,D304-2。
代码
实现代码如下:
package com.example.demoimg.test;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.StandardCopyOption;
public class CopyFilesToDirectories7 {
public static final String nameList = "J313327, J313330";
public static void main(String[] args) {
// 源文件夹
File sourceFolder = new File("C:\\Users\\yf01\\Documents\\img\\123");
File[] files = sourceFolder.listFiles();
// String name11 = "D8129";
if (files != null) {
for (File file : files) {
getFile(file);
}
}
System.out.println("Files copied to directories successfully.");
}
private static void getFile(File file) {
// String name11 = "DG548129";
if (file.isFile()) {
String fileName = file.getName();
String ssName = fileName.substring(0, fileName.lastIndexOf("."));
if (!nameList.contains(ssName)) {
return;
}
//
System.out.println("找到文件");
String folderName = fileName.substring(0, fileName.lastIndexOf("."));
// E:/destination_folder/
File destinationFolder = new File("D:\\img2\\" + folderName);
int index = 1;
File destinationFile = new File("D:\\img2\\" + folderName+ "\\" + fileName);
// 文件夹存在就处理文件名
if (destinationFolder.exists()) {
while (destinationFile.exists()) {
String baseName = fileName.substring(0, fileName.lastIndexOf("."));
String extension = fileName.substring(fileName.lastIndexOf("."));
String newFileName = baseName + "_" + index + extension;
destinationFile = new File("D:\\img2\\" + folderName+ "\\" + newFileName);
index++;
}
}else {
destinationFolder.mkdirs();
}
try {
Files.copy(file.toPath(), destinationFile.toPath(), StandardCopyOption.REPLACE_EXISTING);
} catch (IOException e) {
System.err.println("Error copying file: " + e.getMessage());
}
}else if (file.isDirectory()) {
File[] files = file.listFiles();
if (files != null) {
for (File file2 : files) {
getFile(file2);
}
}
}
}
}
以下是网页下载图片的代码,没用
package com.example.demoimg.test;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import java.io.*;
import java.io.IOException;
import java.net.URL;
import java.net.URLConnection;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;
public class test1 {
public static void main(String[] args) {
for (int i = 2500; i <= 3000; i++) {
// getImg(i);
}
System.out.println("完成");
}
private static void getss() {
System.out.println("开始--" + 1);
String url = "http://test.com/item.html?item_id=" + 1;
try {
Document doc = Jsoup.connect(url).get();
Elements images = doc.select(".goods_box .show-box img");
Elements ulElements = doc.select(".goods_gy ul");
for (int j = 0; j < images.size(); j++) {
Element img = images.get(j);
Element ulElement = ulElements.get(0); // Assuming there is only one ul element
String text = ulElement.text();
int end = text.indexOf("<li>");
String aliasValue = text.substring(end) + "j";
String src = img.attr("src");
System.out.println(aliasValue + "_" + j + ": " + src);
// 下载图片到文件夹
downLoadImg(src, aliasValue);
}
} catch (IOException e) {
System.err.println("Error fetching images: " + e.getMessage());
}
}
private static void getImg(int i) {
System.out.println("开始--" + i);
String url = "http://test.com/item.html?item_id=" + i;
try {
Document doc = Jsoup.connect(url).get();
Elements images = doc.select(".goods_box .show-box img");
Elements ulElements = doc.select(".goods_gy ul");
for (int j = 0; j < images.size(); j++) {
Element img = images.get(j);
Element ulElement = ulElements.get(0); // Assuming there is only one ul element
String text = ulElement.text();
int end = text.indexOf("<li>");
String aliasValue = text.substring(end) + "j";
String src = img.attr("src");
System.out.println(aliasValue + "_" + j + ": " + src);
// 下载图片到文件夹
downLoadImg(src, aliasValue);
}
} catch (IOException e) {
System.err.println("Error fetching images: " + e.getMessage());
}
}
public static void downLoadImg(String imageUrl, String fileName) {
// imageUrl = "http://test/images/8c/29/af/e221b6a084179edcdd0f3875c2b404dc593e4bef.jpg_l.jpg";
String folderPath = "D:/img/";
// String fileName = "A112.jpg";
try {
URL url = new URL(imageUrl);
InputStream in = url.openStream();
Path outputPath = FileSystems.getDefault().getPath(folderPath, fileName);// Path.of(folderPath + fileName);
Files.copy(in, outputPath, StandardCopyOption.REPLACE_EXISTING);
System.out.println("图片下载成功");
} catch (IOException e) {
System.err.println("图片下载失败: " + e.getMessage());
}
}
public static void download(String urlString, String savePath, String filename) throws IOException {
// 构造URL
URL url = new URL(urlString);
// 打开连接
URLConnection con = url.openConnection();
//设置请求超时为20s
con.setConnectTimeout(20 * 1000);
//文件路径不存在 则创建
File sf = new File(savePath);
if (!sf.exists()) {
sf.mkdirs();
}
//jdk 1.7 新特性自动关闭
try (InputStream in = con.getInputStream();
OutputStream out = new FileOutputStream(sf.getPath() + "\\" + filename)) {
//创建缓冲区
byte[] buff = new byte[1024];
int n;
// 开始读取
while ((n = in.read(buff)) >= 0) {
out.write(buff, 0, n);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}