0

Webアプリケーションを実行するためにEclipseJavaEEとtomcatを実行しています。次のコードを使用して、画像ファイルをupload / images/profilepicsディレクトリに保存しました。

public String uploadPhoto() {
    try {
        //get path to upload photo
        String filePath = servletRequest.getSession().
                getServletContext().getRealPath("/uploads/profilepics");

        System.out.println("Server path:" + filePath);

        //creating unique picture name
        Map sess = (Map) ActionContext.getContext().get("session");
        Integer uid = (Integer) sess.get("uid");
        String profilePictureName = uid + "-" + 
                MyUtilityFunctions.createVerificationUrl() + this.userImageFileName;

        //update user record
        //tobe done 
        String imgUrl = filePath + profilePictureName;
        ViewProfileModel pofilePictureUpdate = new ViewProfileModel();
        pofilePictureUpdate.updateUserPhotoUrl(imgUrl, uid);

        //create new File with new path and name
        File fileToCreate = new File(filePath, profilePictureName);

        //copy file to given location and with given name
        FileUtils.copyFile(this.userImage, fileToCreate);
    } catch (Exception e) {
        e.printStackTrace();
        addActionError(e.getMessage());

        return INPUT;
    }
    return SUCCESS;
}

filePathを出力した後、次の結果が得られました。

サーバーパス:/home/bril/webspace/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps/picvik/uploads/profilepics

問題は、画像を取得できないか、同じURLを<img src="">何にも指定しないと表示されないことです。

私が間違っているところを訂正してください。

4

1 に答える 1

1

提案があります:

  1. @DaveNewtonが別の質問で述べたように、この方法でユーザー画像を保存するべきではないという理由はたくさんあります。あなたがあなたの決定をするのを助けるためにいくつかの投稿があります:

    私の個人的な意見は、ユーザーに画像を失わせたくないので、それらをDBに保存することです。

  2. アクセスセッションが必要な場合は、SessionAwareをチェックアウトできます。これは、セッションにアクセスするためのより良い方法です。
  3. アプリケーションコンテナとしてTomcatを使用している場合、ローカルインストールを使用するようにサーバーを構成できます。これにより、この場合の問題の追跡が容易になります。下のこの写真をチェックしてくださいTomcatサーバーの場所

質問に戻ります。これを行うにはさまざまな方法があります。

  • アップロードしたばかりの画像ユーザーが見つからない場合は、手動で確認できます。3を参照してください。
  • そうでなければ、あなたは試すことができます<img src="/uploads/profilepics/<s:property value='profilePictureName'/>"
  • または、ストリームを使用してこの画像を取得できます。スニペットは次のとおりです。

JSP:

    <img src="
        <s:url var="profilePic" action="customer-image-action">
            <s:param name="uid" value="%{uid}"/>
        </s:url>
    " alt="kunden logo" />

アクション:

public String execute() throws Exception {
    // filename = somehow(uid);
    HttpServletRequest request = (HttpServletRequest) ActionContext.getContext().get(ServletActionContext.HTTP_REQUEST);
    imgPath = request.getSession().getServletContext().getRealPath("/uploads/profilepics/")+filename;
    log.debug("context-path: " + imgPath);
    try {
        inputStream = FileUtils.openInputStream(new File(imgPath));
    } catch (IOException e) {
        log.error(e.getCause(), e);
    }
    return SUCCESS;
}
于 2012-12-19T11:02:49.790 に答える