JAVA:无法将图像从一个文件夹复制到位于不同文件夹中的word文档
我需要将文件夹中的所有图像复制到另一个文件夹中的word文档(word文档是在运行时创建的,名称与srcfile相同)。当我运行下面的代码时,没有看到异常,但图像不会复制到word文档。谁能帮帮我吗 代码:JAVA:无法将图像从一个文件夹复制到位于不同文件夹中的word文档,java,selenium,Java,Selenium,我需要将文件夹中的所有图像复制到另一个文件夹中的word文档(word文档是在运行时创建的,名称与srcfile相同)。当我运行下面的代码时,没有看到异常,但图像不会复制到word文档。谁能帮帮我吗 代码: 代码中充满了错误的if语句和错误的逻辑 list[k].isFile()在下面的代码中就足够了。我们正在检查源文件夹中的文件列表是文件还是文件夹。如果有文件,我们可以继续添加它。那么为什么下一个ifif(srcfile.isFile()){?这将始终计算为false,因为srcfile是您的
代码中充满了错误的if语句和错误的逻辑
list[k].isFile()
在下面的代码中就足够了。我们正在检查源文件夹中的文件列表是文件还是文件夹。如果有文件,我们可以继续添加它。那么为什么下一个ifif(srcfile.isFile()){
?这将始终计算为false,因为srcfile是您的源文件夹File srcfile=new文件(“C:\\reports\\test”+fileName()+“\\images”);
“C:\\reports\\test”+fileName()+“\\images\\”+imageName()+”.png“
。我们正在迭代源文件夹文件,并且list[k]
保存图像。因此list[k].getPath()
应获取图像路径
r.addPicture (fis, XWPFDocument.PICTURE_TYPE_PNG,
“C:\reports\test”+fileName()+“\images\“+imageName()+”.png”,
单位.toEMU(200),单位.toEMU(200))If
和else If
做同样的事情。这里需要的是递归逻辑
else if (list[k].isDirectory()){
System.out.println("Directory"+list[k].getName());
if(srcfile.isFile()){
FileInputStream fis = new FileInputStream(srcfile);
XWPFDocument doc = new XWPFDocument(fis);
destfile.getParentFile().mkdir();
更改为destfile.getParentFile().mkdirs();
public static void main(String[] args) throws IOException, InvalidFormatException {
// Create the destination file and folder structure
File destfile = new File("C:\\Evidences\\test\\imgdocx.docx");
destfile.getParentFile().mkdirs();
destfile.createNewFile();
// Create the docx object
XWPFDocument doc = new XWPFDocument();
// Get the source folder and list of files (includes images and
// sub-folders)
File srcfile = new File("C:\\reports\\test\\images");
File[] list = srcfile.listFiles();
System.out.println("Source folder item list " + list.length);
// Iterate through the files in the source folder
for (int k = 0; k < list.length; k++) {
if (list[k].isFile()) {
System.out.println("File - " + list[k].getName());
// Create paragraph
XWPFParagraph p = doc.createParagraph();
XWPFRun r = p.createRun();
// Create fis for image
FileInputStream fis = new FileInputStream(list[k].getPath());
// add image using the fis
r.addPicture(fis, XWPFDocument.PICTURE_TYPE_PNG,
list[k].getPath(),
Units.toEMU(200), Units.toEMU(200));
fis.close();
} else if (list[k].isDirectory()) {
System.out.println("Directory - " + list[k].getName());
// If directory do nothing
// A recursive logic can be implemented to handle the files
// inside folders in source folder
}
}
// write docx to destination file
FileOutputStream fos = new FileOutputStream(destfile);
doc.write(fos);
fos.close();
doc.close();
}
publicstaticvoidmain(字符串[]args)抛出IOException、InvalidFormatException{
//创建目标文件和文件夹结构
File destfile=新文件(“C:\\festions\\test\\imgdocx.docx”);
destfile.getParentFile().mkdirs();
destfile.createNewFile();
//创建docx对象
XWPFDocument doc=新XWPFDocument();
//获取源文件夹和文件列表(包括图像和
//子文件夹)
File srcfile=新文件(“C:\\reports\\test\\images”);
File[]list=srcfile.listFiles();
System.out.println(“源文件夹项列表”+list.length);
//遍历源文件夹中的文件
for(int k=0;k
为什么每次运行都要打开和关闭文件?您应该在循环外打开word文档,循环浏览图像,插入它们并在循环外关闭word文档我已经尝试过了。有一个异常:线程“main”java.io.FileNotFoundException:C:\reports\test463\images中的异常(访问被拒绝)它与Selenium有什么关系?我遗漏了什么吗?如何确保for循环按顺序从srcfile获取图像?目前它正在按如下方式复制:File0.PNG File1.PNG File10.PNG File2.PNG File3.PNG File4.PNG File5.PNG File6.PNG File7.PNG File8.PNG File9.PNG,但我需要按顺序复制它们。你能帮我吗?谢谢adv恩斯!
System.out.println("File"+list[k].getName());
if(srcfile.isFile()){
FileInputStream fis = new FileInputStream(srcfile);
XWPFDocument doc = new XWPFDocument(fis);
r.addPicture (fis, XWPFDocument.PICTURE_TYPE_PNG,
else if (list[k].isDirectory()){
System.out.println("Directory"+list[k].getName());
if(srcfile.isFile()){
FileInputStream fis = new FileInputStream(srcfile);
XWPFDocument doc = new XWPFDocument(fis);
public static void main(String[] args) throws IOException, InvalidFormatException {
// Create the destination file and folder structure
File destfile = new File("C:\\Evidences\\test\\imgdocx.docx");
destfile.getParentFile().mkdirs();
destfile.createNewFile();
// Create the docx object
XWPFDocument doc = new XWPFDocument();
// Get the source folder and list of files (includes images and
// sub-folders)
File srcfile = new File("C:\\reports\\test\\images");
File[] list = srcfile.listFiles();
System.out.println("Source folder item list " + list.length);
// Iterate through the files in the source folder
for (int k = 0; k < list.length; k++) {
if (list[k].isFile()) {
System.out.println("File - " + list[k].getName());
// Create paragraph
XWPFParagraph p = doc.createParagraph();
XWPFRun r = p.createRun();
// Create fis for image
FileInputStream fis = new FileInputStream(list[k].getPath());
// add image using the fis
r.addPicture(fis, XWPFDocument.PICTURE_TYPE_PNG,
list[k].getPath(),
Units.toEMU(200), Units.toEMU(200));
fis.close();
} else if (list[k].isDirectory()) {
System.out.println("Directory - " + list[k].getName());
// If directory do nothing
// A recursive logic can be implemented to handle the files
// inside folders in source folder
}
}
// write docx to destination file
FileOutputStream fos = new FileOutputStream(destfile);
doc.write(fos);
fos.close();
doc.close();
}