.../domains/domain1/images
我的 Glassfish 服务器上有文件夹。在我的 Java Spring 控制器中,我有将上传的图像保存在服务器上的方法:
@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 应用程序中处理绝对路径。