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();
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM