我正在尝试将文件从外部文件夹复制到Java项目文件夹中的本地文件夹。问题是,我不想粘贴本地目标文件夹的确切URL,因为如果将项目移到其他位置,则可能无法正常工作。因此,是否有一种方法可以通过代码(即使在移动时也自动)获取目标文件夹?
这是我用于复制的功能
private static void copyToUpload(File source,String name) throws IOException {
String tail = source.getName().substring(source.getName().lastIndexOf("."));
Files.copy(source.toPath(), (new File("src/"+name+tail)).toPath(), StandardCopyOption.REPLACE_EXISTING);
}
致电:
> protected void doPost(HttpServletRequest request, HttpServletResponse
> response) throws ServletException, IOException {
> File src = new File(request.getParameter("selectedFile"));
> copyToUpload(src, "hello"); }
预期:
src: C:\\..\test.jpg
des: C:\\Test\src\test.jpg
实际:
NoSuchFileException
堆栈跟踪:
> java.nio.file.NoSuchFileException:
> C:\Users\Admin\Downloads\60788720_1251034635090935_8981200640877264896_n.jpg
> -> src\hello.jpg
> sun.nio.fs.WindowsException.translateToIOException(WindowsException.java:79)
> sun.nio.fs.WindowsException.rethrowAsIOException(WindowsException.java:97)
> sun.nio.fs.WindowsFileCopy.copy(WindowsFileCopy.java:205)
> sun.nio.fs.WindowsFileSystemProvider.copy(WindowsFileSystemProvider.java:278)
> java.nio.file.Files.copy(Files.java:1274)
> controller.upload.copyToUpload(upload.java:30)
> controller.upload.doPost(upload.java:78)
> javax.servlet.http.HttpServlet.service(HttpServlet.java:661)
> javax.servlet.http.HttpServlet.service(HttpServlet.java:742)
> org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
答案 0 :(得分:0)
只需将文件放入 src / main / resources 文件夹
您可以使用 ClassLoader.getResource 来获取文件:
String resource = ClassLoader.getSystemClassLoader().getResource("test.jpg").getFile();
File test = new File(resource);
答案 1 :(得分:0)
我已经尝试运行此代码(它具有相似的逻辑)。它运行。我建议检查文件是否存在。
public class CopyFilesTest {
private static Logger logger = LoggerFactory.getLogger(CopyFilesTest.class);
private static Path sourceFile;
private static Path destinationFile;
@BeforeClass
public static void prepareResources() {
sourceFile = new File("src/test/resources/testFile.txt").toPath();
destinationFile = new File("src/test/resources/testFile_copy.txt").toPath();
}
@Test
public void givenExistingFile_expectCopiedFile() {
Assert.assertTrue(sourceFile.toFile().exists());
logger.info(String.format("Source file %s exists.\n", sourceFile.getFileName()));
if (destinationFile.toFile().exists()) {
logger.info(String.format("Destination file %s exists.\n", destinationFile.getFileName()));
} else {
logger.info(String.format("Destination file %s does not exist.\n", destinationFile.getFileName()));
}
logger.info("Copying file.");
Path createdFile = null;
try {
createdFile = Files.copy(sourceFile, destinationFile, StandardCopyOption.REPLACE_EXISTING);
} catch (IOException e) {
logger.warn("Failed copying files.");
}
Assert.assertNotNull(createdFile);
Assert.assertEquals(destinationFile.toAbsolutePath().toString(), createdFile.toAbsolutePath().toString());
logger.info("Files successfully copied.");
}
}
第一次运行的日志中不存在目标文件的地方:
[main] INFO org.stackoverflow.copyfiles.CopyFilesTest - Source file testFile.txt exists.
[main] INFO org.stackoverflow.copyfiles.CopyFilesTest - Destination file testFile_copy.txt does not exist.
[main] INFO org.stackoverflow.copyfiles.CopyFilesTest - Copying file.
[main] INFO org.stackoverflow.copyfiles.CopyFilesTest - Files successfully copied.
Process finished with exit code 0
第二次运行的日志,第一次运行时文件所在的位置:
[main] INFO org.stackoverflow.copyfiles.CopyFilesTest - Source file testFile.txt exists.
[main] INFO org.stackoverflow.copyfiles.CopyFilesTest - Destination file testFile_copy.txt exists.
[main] INFO org.stackoverflow.copyfiles.CopyFilesTest - Copying file.
[main] INFO org.stackoverflow.copyfiles.CopyFilesTest - Files successfully copied.
Process finished with exit code 0