Java Servlet/Jsp 图像上传以及表单值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11217611/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 04:16:59  来源:igfitidea点击:

Java Servlet/Jsp image upload along with form values

javaimageservletsparametersupload

提问by Tsega Eb

I have a jsp form that accepts details about Employee name, sex, age, E-mail address and a

我有一个 jsp 表单,它接受有关员工姓名、性别、年龄、电子邮件地址和

回答by Ramesh PVK

Servlet 3.0 container'shas standard support for multipart data. First you should be writing a HTML page which takes the file input along with other input parameters.

Servlet 3.0 容器标准支持多部分数据。首先,您应该编写一个 HTML 页面,该页面接受文件输入以及其他输入参数。

<form action="uploadservlet" method="post" enctype="multipart/form-data">
    <input type="text" name="name" />
    <input type="text" name="age" />
    <input type="file" name="photo" />
    <input type="submit" />
</form>

Now write a UploadServlet which uses the Servlet 3.0 Upload API. Here is the code which demonstrates the usage of API. Fist the servlet handling multipart data should define MultiPartConfig using any of the two approaches:

现在编写一个使用 Servlet 3.0 Upload API 的 UploadServlet。这是演示 API 用法的代码。首先,处理多部分数据的 servlet 应该使用以下两种方法中的任何一种来定义 MultiPartConfig:

  • @MultiPartConfigannotation on Servlet Class
  • In web.xml,by adding <multipart-config>entry inside <servlet>definition.
  • @MultiPartConfigServlet 类的注解
  • web.xml,加入<multipart-config>中的条目<servlet>定义。

Here is the UploadServlet,

这是 UploadServlet,

@MultipartConfig
 public class UploadServlet extends HttpServlet
 {
   protected void service(HttpServletRequest request, 
       HttpServletResponse responst) throws ServletException, IOException
   {
      Collection<Part> parts = request.getParts();
      if (parts.size() != 3) {
         //can write error page saying all details are not entered
       }

       Part filePart = httpServletRequest.getPart("photo");
       InputStream imageInputStream = filePart.getInputStream();
       //read imageInputStream
       filePart.write("somefiepath");
       //can also write the photo to local storage

       //Read Name, String Type 
       Part namePart = request.getPart("name");
       if(namePart.getSize() > 20){
           //write name cannot exceed 20 chars
       }
       //use nameInputStream if required        
       InputStream nameInputStream = namePart.getInputStream();
       //name , String type can also obtained using Request parameter 
       String nameParameter = request.getParameter("name");

       //Similialrly can read age properties
       Part agePart = request.getPart("age");
       int ageParameter = Integer.parseInt(request.getParameter("age"));



    }

}

If you are not using Sevlet 3.0 Container, you should be truing Apache Commons File Upload. Here are the links for using Apache Commons File Upload:

如果您没有使用 Sevlet 3.0 Container,您应该使用 Apache Commons File Upload。以下是使用 Apache Commons File Upload 的链接:

References:

参考: