我想将上传的文件保存在 tomcat 目录中,之后我想让一些用户访问下载这些文件。现在文件保存在 eclipse 目录中,或者保存在 eclipse 的磁盘上,但从不在 tomcat 目录中。我不知道如何访问 tomcat 的路径,我不想给出:C:\tomcat7\webapps\blabla\,我想获得 tomcat 所在的路径。
上传控制器:
@Controller
@RequestMapping(value = "/upload")
public class UploadController
{
@RequestMapping(method = RequestMethod.GET)
public String getUploadForm(Model model)
{
model.addAttribute(new UploadItem());
return "upload/uploadForm";
}
@RequestMapping(method = RequestMethod.POST)
public String create(UploadItem uploadItem, BindingResult result)
{
if (result.hasErrors())
{
for(ObjectError error : result.getAllErrors())
{
System.err.println("Error: " + error.getCode() + " - " + error.getDefaultMessage());
}
return "upload/uploadForm";
}
// Some type of file processing...
String fileNameToCreate = "/"+uploadItem.getFileData().getOriginalFilename();
File file;
try {
file = new File(fileNameToCreate);
FileUtils.writeByteArrayToFile(file, uploadItem.getFileData().getBytes());
} catch (Throwable e) {
e.printStackTrace();
}
System.err.println("-------------------------------------------");
System.err.println("Test upload: " + uploadItem.getName());
System.err.println("Test upload: " + uploadItem.getFileData().getOriginalFilename());
System.err.println("-------------------------------------------");
return "redirect:/app/";
}
}
我希望有人有一些想法来获取从这个控制器到 tomcat 的路径,或者我如何修复它