我想提取文件路径然后可以上传到谷歌驱动器。如何从MultipartFile获取路径?
//logger.info(""+uploadFile.getClass().getResource("").getFile().getPath());
//System.out.println(uploadFile.getClass().getResource("").getPath());
还尝试将MultipartFile转换为File但由于“import com.google.api.services.drive.model.File;”而无法“导入java.io.File”无法兼容。我认为。已经使用了drive.model.File了很多。
@Override
public int uploadFile(HttpSession session, MultipartFile uploadFile) throws Exception {
// TODO Auto-generated method stub
File fileMetadata = new File();
Drive driveService = getCredential(session);
fileMetadata.setName(uploadFile.getOriginalFilename());
logger.info(uploadFile.getResource().getFile().getAbsoluteFile().getPath());
//logger.info(""+uploadFile.getClass().getResource("").getFile().getPath());
//System.out.println(uploadFile.getClass().getResource("").getPath());
java.io.File filePath = new java.io.File("need to input file path here");
FileContent mediaContent = new FileContent("image/jpeg", filePath);
File file = driveService.files().create(fileMetadata, mediaContent).setFields("id").execute();
System.out.println("File ID: " + file.getId());
return 0;
}
带有path []的上下文中servlet [appServlet]的Servlet.service()引发了异常java.io.FileNotFoundException:MultipartFile资源[uploadFile]无法解析为绝对文件路径
我可以从参数中的MultipartFile获取名称,大小,因此它存在,但我怎样才能从中获取路径?
您无需文件路径即可将文件上传到任何位置。请改用multipartFile.getInputStream()
或multipartFile.getBytes()
。实际上,在获取文件的那一刻,你无法知道文件的路径(它不是一个可靠的规则,而是一个依赖于浏览器的功能)