我有1个带有doPost方法的Servlet,它将上传图像。但是我需要先创建一个Pattern对象,这样我才能获得这个模式的ID,并使用它来使它成为图像的名称。 (对这个问题不感兴趣,但现在你知道我的动机了)
这是我的名为CreatePatternServlet
的Servlet,它使用了doGet:
public void doGet(HttpServletRequest req,
HttpServletResponse resp)
throws ServletException, java.io.IOException {
/*--------------------creating the pattern-------------------------*/
//TODO make pattern
String name = req.getParameter("name");
String scope = req.getParameter("scope");
String purpose = req.getParameter("purpose");
String problem = req.getParameter("problem");
String solution = req.getParameter("solution");
System.out.println("step 1: ["+name+","+ scope+","+purpose+","+problem+","+solution+"]");
//This method returns the id of the pattern, which will be used for the name of the image
int patternId = Controller.createPattern(name, scope, purpose, problem, solution);
/*------------------end of creating pattern-----------------------*/
这将是UploadImageServlet
中的doPost:
doPost(HttpServletRequest req, HttpServletRespose resp) throws ServletException, IOException{
isMultipart = ServletFileUpload.isMultipartContent(req);
java.io.PrintWriter out = resp.getWriter( );
if( !isMultipart ){
return;
}
DiskFileItemFactory factory = new DiskFileItemFactory();
// maximum size that will be stored in memory
factory.setSizeThreshold(maxMemSize);
// Location to save data that is larger than maxMemSize.
factory.setRepository(new File(repository));
// Create a new file upload handler
ServletFileUpload upload = new ServletFileUpload(factory);
// maximum file size to be uploaded.
upload.setSizeMax( maxFileSize );
try{
// Parse the request to get file items.
List fileItems = upload.parseRequest(req);
//calling the function which loads the image in the default directory
Controller.uploadImage(fileItems, patternId);
}catch(Exception e){
System.out.println("oeps");
}
/*--------------------ending creation of image, image is created--------------------*/
}
他们独立工作完全没问题,但我想将它们结合起来。 uploadImage无法与doGet一起运行,因此我需要一些方法将Get请求转发给Post请求。这可能吗?或者我应该找到解决方法吗?
答案 0 :(得分:0)
开始阅读并理解这篇文章:How to upload files to server using JSP/Servlet?
一旦你这样做了,你应该能够用doPost中的适当调用替换你在doGet()中所做的事情,以便一起修补它。