【发布时间】:2020-04-08 14:13:52
【问题描述】:
所以我正在尝试创建一个方法,通过该方法我将上传一个文件。但是在此方法中创建一个新文件时,我需要使用 getBytes() 方法。但是我收到错误提示。
The method getBytes() is undefined for the type File
我怎样才能克服这个??下面是我的方法。
public String SomeFile(File file, String basePath, UrlInfoKey urlInfoKey) {
LOG.info("basePath for back up is {}", basePath);
String uploadedPath = null;
String fileDetails = file.getName();
String locationDetails = fileDetails;
String locationFolderPath = basePath+"/"+locationDetails;
File directory = new File(locationFolderPath);
if(!directory.isDirectory()) {
LOG.info("creating the directory at {}", locationFolderPath);
directory.mkdir();
}
String filepath = locationFolderPath+"/"+file.getName().trim();
LOG.info("file path to back up is {}",filepath);
File creatingFile = new File(filepath);
try {
creatingFile.createNewFile();
FileOutputStream fos = new FileOutputStream(creatingFile);
fos.write(file.getBytes());
fos.close();
uploadedPath = filepath;
} catch (Exception e) {
LOG.info("could not create file ", e.getMessage());
}
uploadedPath= updateURLWithDBURL(urlInfoKey,uploadedPath);
LOG.info("file uploaded at location {}",uploadedPath);
return uploadedPath;
}
Line:fos.write(file.getBytes());
这是我遇到错误的地方。我可以使用其他替代方法吗。提前致谢。
【问题讨论】:
-
嗨 Lara,这可能对你有帮助 stackoverflow.com/questions/2520305/…
-
嗨 Pankaj,我看到了您分享的解决方案,但我认为这不是我想要的。如果您对我的代码中的问题有任何其他替代方案,请分享。谢谢
-
您使用 Java 的旧文件 io 类而不是围绕
Files和Paths旋转的现代 API NIO 是否有特定原因?使用起来要简单得多。 -
没有
File.getBytes()这样的方法。请参阅 Javadoc。不清楚你在问什么。