我的 Glassfish 服务器上有文件夹 .../domains/domain1/images
。在我的 Java Spring Controller 中,我有将上传的图像保存在服务器上的方法:
@RequestMapping(value = "/form", method = RequestMethod.POST)
public String sendData(@RequestParam("north") Double north, @RequestParam("east") Double east, @RequestParam("phone") String phone, @RequestParam("image") MultipartFile file, HttpServletRequest request, ModelMap model) {
if (!"image/jpg".equals(file.getContentType()) && !"image/jpeg".equals(file.getContentType())) {
return "redirect:/form";
}
Date date = new Date();
Timestamp ts = new Timestamp(date.getTime());
String filename = ts.toString();
filename = filename.replaceAll("\\s", "_");
String path = imagePath + filename;
File imageFile = new File(path);
try {
BufferedImage image = ImageIO.read(file.getInputStream());
image = ImageTransformer.scaleByWidth(image, imageWidth);
imageFile.createNewFile();
ImageIO.write(image, "jpg", imageFile);
} catch (IOException e) {
e.printStackTrace();
return "redirect:/form";
}
Marker m = new Marker();
m.setNorth(north);
m.setEast(east);
m.setPhone(phone);
m.setUrl(filename);
markerService.add(m);
return "redirect:/start";
}
我应该如何设置我的imagePath
变量来实现它?我应该更改服务器配置中的某些默认路径吗?
提前致谢。
UPD:如果可能的话,不想在 Java 应用程序中处理绝对路径。
最佳答案
在 WEB-INF/glassfish-web.xml 中,尝试为您的资源添加备用文档根:
<glassfish-web-app error-url="">
<context-root>/yourapp</context-root>
...
<property name="alternatedocroot_1" value="from=/images/* dir=/path/to/your/images"/>
<property name="alternatedocroot_2" value="from=/sounds/* dir=/path/to/your/sounds"/>
</glassfish-web-app>
然后,在您的应用程序代码中,您可以使用相对于应用程序根目录的路径。
关于java - 从 Java Spring MVC 3 应用程序访问 Glassfish 文件夹,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15642783/