zoukankan      html  css  js  c++  java
  • CommonsMultipartFile---用Spring实现文件上传

    CommonsMultipartFile

    Spring提供的读取文件的类,使用方便,依赖spring-web-3.1.2.RELEASE.jar

    包路径:

    java.lang.Object

      extended by org.springframework.web.multipart.commons.CommonsMultipartFile

    方法汇总:
     
    byte[] getBytes()  Return the contents of the file as an array of bytes.
    String getContentType()  Return the content type of the file.
    FileItem getFileItem() Return the underlying org.apache.commons.fileupload.FileItem instance
    InputStream getInputStream() Return an InputStream to read the contents of the file from.
    String getName() Return the name of the parameter in the multipart form.
    String getOriginalFilename()   Return the original filename in the client's filesystem.
    long getSize() Return the size of the file in bytes.
    String getStorageDescription()  Return a description for the storage location of the multipart content.
    protected  boolean isAvailable() Determine whether the multipart content is still available.
    boolean isEmpty()   Return whether the uploaded file is empty, that is, either no file has been chosen in the multipart form or the chosen file has no content.
    voic transferTo(File dest)    Transfer the received file to the given destination file.

     使用方法:

    1.spring配置文件配置文件上传解析器
        <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">         <property name="defaultEncoding" value="utf-8"></property>         <property name="maxUploadSize" value="90000000" />         <property name="uploadTempDir" value="uploadFiles"></property>     </bean>
    2.html写法注意两点
        a.input类型为file:<input type="file" name="sealPfxFile"  id="sealPfxFile" size="24" />
        b.form中增加参数enctype="multipart/form-data":
           <form id="addSeal" name="addSeal" action="${root}/seal/o_add.do" enctype="multipart/form-data" method="post">
     
    3.Service的写法(注意与html中定义的名称相同即可通过get方法取得需要的内容)
    public String doAction(@RequestParam("sealPfxFile") CommonsMultipartFile sealPfxFile, Seal seal, ModelMap modelMap, HttpServletRequest request) throws Exception {
          //上传文件名
          String fileName = sealPfxFile.getFileItem().getName();
      //上传文件流
          InputStream is = sealPfxFile.getInputStream();
    }
  • 相关阅读:
    七牛上传图片视频demo
    JavaScript数组及相关方法
    Math对象产生随机数一个小应用
    JavaScript 开发进阶:理解 JavaScript 作用域和作用域链
    HTML5 中的meter 标签的样式设置
    jQuery报错:Uncaught ReferenceError: $ is not defined
    每次打开office 2013都提示配置进度,必须得等他下完然后重启,重启完了在打开,还是提示配置进度,怎么解决
    CSS小技巧收藏
    DOM中元素节点、属性节点、文本节点的理解13.3
    (转)如果知道dll文件是面向32位系统还是面向64位系统的?
  • 原文地址:https://www.cnblogs.com/lin-bear/p/5193096.html
Copyright © 2011-2022 走看看