gpt4 book ai didi

java - 我希望我的文件路径适用于使用 Java 的 linux 和 Windows 环境

转载 作者:太空宇宙 更新时间:2023-11-04 12:12:32 26 4
gpt4 key购买 nike

我正在编写一个 java 代码来创建工作空间目录以 checkin 单个或多个文件。从 jsp 页面接收文件名下面是用于创建目录和文件路径的代码片段。

try {
File uploadedFile = null;
String tmpDir = context.createWorkspace();
String filePath = tmpDir + java.io.File.separator;
//System.out.println("filePath"+ filePath);
//String filePath1 = tmpDir + java.util.regex.Pattern.quote(File.separator);
//System.out.println("filePath1 "+ filePath1);
DiskFileItemFactory factory = new DiskFileItemFactory();
factory.setRepository(new File(tmpDir));
ServletFileUpload upload = new ServletFileUpload(factory);
List<FileItem> items = upload.parseRequest(request);
HashMap hmRequest = new HashMap();
String sFileName = DomainConstants.EMPTY_STRING;
String sParamKey = DomainConstants.EMPTY_STRING;
String sParamValue = DomainConstants.EMPTY_STRING;
StringBuffer fileNameList = new StringBuffer();
StringBuffer nameList = new StringBuffer();
StringBuffer uploadfileNameList = new StringBuffer();
StringBuffer uploadnameList = new StringBuffer();
StringBuffer coverletterfileNameList = new StringBuffer();
StringBuffer coverletternameList = new StringBuffer();

if (null != items && items.size() > 0)
{
Iterator<FileItem> iter = items.iterator();

while (iter.hasNext())
{
FileItem item = iter.next();
if (item.isFormField())
{
sParamKey = item.getFieldName();
sParamValue = item.getString();
hmRequest.put(sParamKey, sParamValue);
}
}

Iterator<FileItem> iter1 = items.iterator();
while (iter1.hasNext())
{
FileItem item1 = iter1.next();
if (!item1.isFormField())
{
//String sFileName1=null;
sFileName = item1.getName();
String fieldname = item1.getFieldName();
if (UIUtil.isNotNullAndNotEmpty(sFileName) && fieldname.equals("coverLetterFile"))
{
sFileName = sFileName.substring(sFileName.lastIndexOf("\\") + 1);
sFileName =
System.out.println("sFileName"+ sFileName);
//sFileName1 = sFileName.substring(sFileName.lastIndexOf(java.util.regex.Pattern.quote(File.separator)) + 1);
//System.out.println("sFileName1111"+ sFileName1);
uploadedFile = new File(tmpDir + File.separator + sFileName);
item1.write(uploadedFile);
coverletterfileNameList.append(tmpDir + File.separator + sFileName);
coverletterfileNameList.append(",");
coverletternameList.append(sFileName);
coverletternameList.append(",");
}

当我在 Windows 上工作时,下行是否会在 linux 环境中造成问题。任何人都可以为此提出解决方案吗?

sFileName = sFileName.substring(sFileName.lastIndexOf("\") + 1);

最佳答案

使用 File.separator,它为您的系统环境填充正确的分隔符,而不是硬编码的字符串。

关于java - 我希望我的文件路径适用于使用 Java 的 linux 和 Windows 环境,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48439467/

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