1

.../domains/domain1/imagesGlassfish サーバーにフォルダーがあります。私の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 アプリケーションで絶対パスを扱いたくありません。

4

1 に答える 1