gpt4 book ai didi

java - FileItem.getName() 返回文件的所有路径,而不仅仅是 Java 中的文件名

转载 作者:行者123 更新时间:2023-12-01 22:04:12 24 4
gpt4 key购买 nike

我在获取文件名时遇到一些问题。它总是返回我的文件的所有路径,而不仅仅是文件名

List<FileItem> fileItemsList = uploader.parseRequest(request);
Iterator<FileItem> fileItemsIterator = fileItemsList.iterator();
while(fileItemsIterator.hasNext()){
FileItem fileItem = fileItemsIterator.next();
System.out.println("FileName="+fileItem.getName());

输出为FileName=C:\Users\Administrator\Downloads\demoUpload2.war 但我只需要 demoUpload2.war

最佳答案

Why does FileItem.getName() return the whole path, and not just the file name? Internet Explorer provides the entire path to the uploaded file and not just the base file name. Since FileUpload provides exactly what was supplied by the client (browser), you may want to remove this path information in your application. You can do that using the following method from Commons IO (which you already have, since it is used by FileUpload). String fileName = item.getName(); if (fileName != null) { filename = FilenameUtils.getName(filename); }

引用号:http://commons.apache.org/proper/commons-fileupload/faq.html#whole-path-from-IE你可以这样做:

String fileName = item.getName();
if (fileName != null) {
filename = FilenameUtils.getName(filename);
}

关于java - FileItem.getName() 返回文件的所有路径,而不仅仅是 Java 中的文件名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33093641/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com