Java Servlet/Jsp 图像与表单值一起上传

编程入门 行业动态 更新时间:2024-10-26 04:27:59
本文介绍了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 container's 标准支持多部分数据.首先,您应该编写一个 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 Upload 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 Servlet 类上的注解
  • 在 web.xml 中, 通过在 <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 Container,您应该使用 Apache Commons File Upload.以下是使用 Apache Commons File Upload 的链接:

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:

  • 使用 Apache Commons 文件上传
  • Apache Commons 文件上传示例

参考资料:

  • Servlet 3.0 javax.servlet.http.HttpServletRequest API
  • Servlet 3.0 javax.servlet.http.Part API
  • 使用 Servlet 3.0 上传文件

更多推荐

Java Servlet/Jsp 图像与表单值一起上传

本文发布于:2023-10-09 18:03:59,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1476401.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:表单   图像   上传   Java   Servlet

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!