【发布时间】:2014-02-05 23:14:18
【问题描述】:
我在本地 Tomcat 的服务器上工作过,现在我刚刚将我的应用程序部署到 CloudBees。 除了将文件上传到服务器目录之外,一切都很好。
我的控制器中有这样一段代码,它在本地主机上对我有用:
@RequestMapping("/main/admin/upload")
public ModelAndView fileUploaded(
@ModelAttribute("uploadedFile") FileUpload uploadedFile,
final HttpServletRequest request,
final HttpServletResponse response, BindingResult result) {
InputStream inputStream = null;
OutputStream outputStream = null;
String pdfDirectory = uploadedFile.getPdfDirectory();
MultipartFile file = uploadedFile.getFile();
String fileName = file.getOriginalFilename();
try {
inputStream = file.getInputStream();
String pdfFileDirectory = request.getSession().getServletContext()
.getRealPath("/")
+ "resources\\pdf\\" + pdfDirectory + "\\";
File newFile = new File(pdfFileDirectory + fileName);
if (!newFile.exists()) {
newFile.createNewFile();
}
outputStream = new FileOutputStream(newFile);
int read = 0;
byte[] bytes = new byte[1024];
while ((read = inputStream.read(bytes)) != -1) {
outputStream.write(bytes, 0, read);
}
} catch (IOException e) {
e.printStackTrace();
}
return new ModelAndView("redirect:/main/admin");
}
我不确定这条线是否正常工作:
request.getSession().getServletContext().getRealPath("/")
我确定文件夹已创建,因为我已在我的 Eclipse 项目中手动添加它们。在解压到 CloudBees 之前,我还手动将一些文件添加到这些文件夹中,并且这些文件可以访问,但是当我尝试使用上述代码上传文件时,它不起作用。
【问题讨论】:
-
我发现了一件事......我不确定是否有效的行返回:/mnt/genapp/apps/7c7c897e/staxcat/install/webapp.war/resources/pdf/Facebook/有没有办法将文件添加到提取到war文件的应用程序中?
标签: jakarta-ee spring-mvc file-upload cloudbees