0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

画像の保存と紐付けの同時アップロード

Last updated at Posted at 2018-11-12

##画像とテキストを紐付けしながら保存するにあたって

CreateServlet
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String _token = getParamVal(request.getPart("_token")); // = (String) request.getParameter("_token");

        if (_token != null && _token.equals(request.getSession().getId())) {
            EntityManager em = DBUtil.createEntityManager();

            Reshipi r = new Reshipi(); 
            Picture p = new Picture(); 

            String name = getParamVal(request.getPart("name")); // = request.getParameter("name"); 
            r.setName(name); 

            String content = getParamVal(request.getPart("content")); // = request.getParameter("content");
            r.setContent(content);

            Timestamp currentTime = new Timestamp(System.currentTimeMillis());
            r.setCreated_at(currentTime);
            r.setUpdated_at(currentTime);

            //名前の取得
            String file_name = getParamVal(request.getPart("file_name")); 
            p.setFile_name(file_name);
            //レシピのIDの取得
            Reshipi reshipi_id = getParamVal(request.getPart("reshipi_id"));
            p.setReshipi_id(reshipi_id);
            //pictureのIdの取得
            Integer id = getParmVal(request.getPart("id"));
            p.setId(id);
            //エラーリストの作成
            List<String> errors = ReshipiValidator.validate(r);
            //もしエラーがあったら
            if (errors.size() > 0) {
                //DBを閉じる
                em.close();
                
                request.setAttribute("_token", request.getSession().getId());
                request.setAttribute("content", r);
                request.setAttribute("errors", errors);

                response.sendRedirect(request.getContextPath() + "/index");
                return;
            } else {
                //データベースへの保存
                em.getTransaction().begin();
                em.persist(r);
                em.getTransaction().commit();
                request.getSession().setAttribute("flush", "登録が完了しました");
                em.close();
            }
            //画像フォームから受け取った画像処理
            request.setCharacterEncoding("UTF-8");
            response.setContentType("text/html; charset=UTF-8");
            Collection<Part> parts = request.getParts();

            for (Part part : parts) {
                String f_name = getFileName(part);
                if(!f_name.contains("no_p_name") && !f_name.isEmpty()){

                part.write(getServletContext().getRealPath("/images") + "/" + f_name);
                response.getWriter().append("アップロード:").append(f_name);
                }

            }

と書いていたが、実際は

CreateServlet

    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String _token = getParamVal(request.getPart("_token")); // = (String) request.getParameter("_token");

        if (_token != null && _token.equals(request.getSession().getId())) {
            EntityManager em = DBUtil.createEntityManager();

            Reshipi r = new Reshipi();
            Picture p = new Picture();

            String name = getParamVal(request.getPart("name")); // = request.getParameter("name");
            r.setName(name);

            String content = getParamVal(request.getPart("content")); // = request.getParameter("content");
            r.setContent(content);

            Timestamp currentTime = new Timestamp(System.currentTimeMillis());
            r.setCreated_at(currentTime);
            r.setUpdated_at(currentTime);

            String file_name = getParamVal(request.getPart("file_name"));
            p.setFile_name(file_name);

            //画像とreshipi_idとの紐付け★★★★★★★★★★★★★
            p.setReshipi_id(r);

            //画像フォームから受け取った画像処理↓★★★★★★★★★★★★★★★
            //上のコードでは下の方にあったが、保存処理等の下にあっても正しく機能しない
            //DBへの保存コードの上にあることで初めて機能する
            request.setCharacterEncoding("UTF-8");
            response.setContentType("text/html; charset=UTF-8");
            Collection<Part> parts = request.getParts();

            for (Part part : parts) {
                String f_name = getFileName(part);
                if(!f_name.contains("no_p_name") && !f_name.isEmpty()){

                part.write(getServletContext().getRealPath("/images") + "/" + f_name);
                response.getWriter().append("アップロード:").append(f_name);
                p.setFile_name(f_name);
                }
            }
            List<String> errors = ReshipiValidator.validate(r);
            if (errors.size() > 0) {
                em.close();

                request.setAttribute("_token", request.getSession().getId());
                request.setAttribute("content", r);
                request.setAttribute("errors", errors);

                response.sendRedirect(request.getContextPath() + "/index");
                return;
            } else {
                //保存処理
                em.getTransaction().begin();
                em.persist(r);
                //画像の保存処理を入れる★★★★★★★★★★★★★★★★★★★★★
                em.persist(p);
                em.getTransaction().commit();
                request.getSession().setAttribute("flush", "登録が完了しました");
                em.close();
            }

となる。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?