Java Servlet / Jsp图像上传以及表单值 [英] Java Servlet/Jsp image upload along with form values

查看:98
本文介绍了Java Servlet / Jsp图像上传以及表单值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

推荐答案

Servlet的详细信息3.0容器具有对多部分数据的标准支持。首先,您应该编写一个HTML页面,其中包含文件输入以及其他输入参数。

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

<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>

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

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:


  • @MultiPartConfig 注释

  • web.xml中,添加< multipart-config> < servlet> 定义内的条目。

  • @MultiPartConfig annotation on Servlet Class
  • In web.xml, by adding <multipart-config> entry inside <servlet> definition.

这是UploadServlet,

Here is the 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"));



    }

}

如果你没有使用Sevlet 3.0容器,你应该完善Apache Commons File Upload。以下是使用Apache Commons文件上传的链接:

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:

  • Using Apache Commons File Upload
  • Apache Commons File Upload Example

参考文献:

  • Servlet 3.0 javax.servlet.http.HttpServletRequest API
  • Servlet 3.0 javax.servlet.http.Part API
  • Uploading File using Servlet 3.0

这篇关于Java Servlet / Jsp图像上传以及表单值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆