如何检索上传的图像并保存到JSP文件? [英] How to retrieve uploaded image and save to a file with JSP?

查看:127
本文介绍了如何检索上传的图像并保存到JSP文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何检索上传的图片并保存到jsp文件?


创建一个web项目
  • 创建一个至少包含以下内容:

     < form action =uploadmethod =postenctype =multipart / form-data> ; 
    < input type =filename =file>
    < input type =submit>
    < / form>


  • 转至 Apache Commons FileUpload主页阅读 用户指南常见问题部分。 / b>

  • 下载以下库的二进制文件

  • >
    解压缩zip文件,并将JAR文件放在您的web项目的 / WEB-INF / lib 中。


  • 创建一个至少包含以下内容的Servlet类:

      publi c类UploadServlet扩展HttpServlet {
    @Override $ b $保护无效doPost(HttpServletRequest请求,HttpServletResponse响应)抛出ServletException异常IOException {
    List< FileItem> items = null;
    尝试{
    items = new ServletFileUpload(new DiskFileItemFactory())。parseRequest(request);
    catch(FileUploadException e){
    throw new ServletException(Can not parse multipart request。,e); (FileItem item:items){
    if(item.isFormField()){
    //这里处理常规表单字段的方式与request.getParameter() 。
    //您可以通过item.getFieldName()获取参数名称;
    //你可以通过item.getString()获取参数值;
    } else {
    //在这里处理上传的字段。
    String filename = FilenameUtils.getName(item.getName()); //获取文件名。
    File file = new File(/ path / to / uploads,filename); //定义目标文件
    item.write(file); //写入目标文件
    }
    }
    //显示结果页面。
    request.getRequestDispatcher(result.jsp)。forward(request,response);



    $ b $

    将web.xml中的servlet映射为如下:

     < servlet> 
    < servlet-name>上传< / servlet-name>
    < servlet-class> mypackage.UploadServlet< / servlet-class>
    < / servlet>
    < servlet-mapping>
    < servlet-name>上传< / servlet-name>
    < url-pattern> / upload< / url-pattern>
    < / servlet-mapping>


  • 就是这样。在JSP中提交表单时,它将调用与< url-pattern> / upload >的servlet,然后servlet将在 doPost()方法中完成它的任务。最后,这一切都很简单。希望这有助于。


    Sorry but I'm totally new to jsp.

    How to retrieve uploaded image and save to a file with jsp?

    解决方案

    1. Create a web project.
    2. Create a JSP file with at least the following content:

      <form action="upload" method="post" enctype="multipart/form-data">
          <input type="file" name="file">
          <input type="submit">
      </form>
      

    3. Go to Apache Commons FileUpload homepage, read both the User Guide and Frequently Asked Questions sections.

    4. Download the binaries of the following libraries:

    5. Unpack the zips and place the JAR files in the /WEB-INF/lib of your web project.

    6. Create a Servlet class with at least the following content:

      public class UploadServlet extends HttpServlet {
          @Override
          protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
              List<FileItem> items = null;
              try {
                  items = new ServletFileUpload(new DiskFileItemFactory()).parseRequest(request);
              } catch (FileUploadException e) {
                  throw new ServletException("Cannot parse multipart request.", e);
              }
              for (FileItem item : items) {
                  if (item.isFormField()) {
                      // Process regular form fields here the same way as request.getParameter().
                      // You can get parameter name by item.getFieldName();
                      // You can get parameter value by item.getString();
                  } else {
                      // Process uploaded fields here.
                      String filename = FilenameUtils.getName(item.getName()); // Get filename.
                      File file = new File("/path/to/uploads", filename); // Define destination file.
                      item.write(file); // Write to destination file.
                  }
              }
              // Show result page.
              request.getRequestDispatcher("result.jsp").forward(request, response);
          }
      }
      

    7. Map the servlet in web.xml as follows:

      <servlet>
          <servlet-name>upload</servlet-name>
          <servlet-class>mypackage.UploadServlet</servlet-class>
      </servlet>
      <servlet-mapping>
          <servlet-name>upload</servlet-name>
          <url-pattern>/upload</url-pattern>
      </servlet-mapping>
      

    That should be it. When you submit the form in the JSP, it will invoke the action /upload which matches the <url-pattern> of the servlet and then the servlet will do its task in the doPost() method. At end it's all fairly simple. Hope this helps.

    这篇关于如何检索上传的图像并保存到JSP文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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