##POSTサーブレットにアップロード用のボタンの設置
Upload.java
package pakage;
@WebServlet("/upload")
@MultipartConfig(location="/tmp",maxFileSize=1048576)
public class UploadServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//partという変数を作る
Part part = request.getPart("file");
//part(主にjsp)から送られてきたファイル名を取得
String name = this.getFileName(part);
part.write(getServletContext().getRealPath("/WEB-INF/lib/upload")+ "/" + name);
response.sendRedirect("jsp/upload.jsp");
}
private String getFileName(Part part){
String name = null;
for(String dispotion : part.getHeader("Content-Disposition").split(";")){
if(dispotion.trim().startsWith("filename")){
name = dispotion.substring(dispotion.indexOf("=") + 1).replace("\"","").trim();
name = name.substring(name.lastIndexOf("\\") + 1 );
break;
}
}
return name;
}
}
CreateServlet.java
package controllers;
@WebServlet("/create")
public class CreateServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String _token = (String)request.getParameter("_token");
if(_token != null && _token.equals(request.getSession().getId())){
EntityManager em = DBUtil.createEntityManager();
Reshipi r = new Reshipi();
String name = request.getParameter("name");
r.setName(name);
String content = request.getParameter("content");
r.setContent(content);
Timestamp currentTime = new Timestamp(System.currentTimeMillis());
r.setCreated_at(currentTime);
r.setUpdated_at(currentTime);
em.getTransaction().begin();
em.persist(r);
em.getTransaction().commit();
em.close();
response.sendRedirect(request.getContextPath() + "/index");
}
}
}
この二つを結合する場合、
package controllers;
@WebServlet("/create")
public class CreateServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
public CreateServlet() {
super();
// TODO Auto-generated constructor stub
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String _token = (String)request.getParameter("_token");
if(_token != null && _token.equals(request.getSession().getId())){
EntityManager em = DBUtil.createEntityManager();
Reshipi r = new Reshipi();
String name = request.getParameter("name");
r.setName(name);
String content = request.getParameter("content");
r.setContent(content);
Part part = request.getPart("file");
String filename = this.getFileName(part);
Timestamp currentTime = new Timestamp(System.currentTimeMillis());
r.setCreated_at(currentTime);
r.setUpdated_at(currentTime);
em.getTransaction().begin();
em.persist(r);
em.getTransaction().commit();
em.close();
response.sendRedirect(request.getContextPath() + "/index");
}
}
private String getFileName(Part part){
String filename = null;
for(String dispotion : part.getHeader("Content-Disposition").split(";")){
if(dispotion.trim().startsWith("filename")){
filename = dispotion.substring(dispotion.indexOf("=") + 1).replace("\"", "").trim();
filename = filename.substring(filename.lastIndexOf("\\") + 1);
break;
}
}
return filename;
}
}
private String getFileName(Part part)
メソッドは必ずPOSTメソッドの内部ではなく、外部に書く。POSTメソッドにあったものはPOSTメソッドへ、GETメソッドにあったものはGETメソッドに入れる。