使用ocupload插件上传文件

CommonsMultipartFile---用Spring实现文件上传

CommonsMultipartFile

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

包路径:

java.lang.Object

  extended byorg.springframework.web.multipart.commons.CommonsMultipartFile

方法汇总:

 

byte[]getBytes() Return the contents of the file as an array of bytes.
StringgetContentType() Return the content type of the file.
FileItemgetFileItem()Return the underlying org.apache.commons.fileupload.FileItem instance
InputStreamgetInputStream()Return an InputStream to read the contents of the file from.
StringgetName()Return the name of the parameter in the multipart form.
StringgetOriginalFilename()  Return the original filename in the client's filesystem.
longgetSize()Return the size of the file in bytes.
StringgetStorageDescription() Return a description for the storage location of the multipart content.
protected  booleanisAvailable()Determine whether the multipart content is still available.
booleanisEmpty()  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.
voictransferTo(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();

}

 

原创:https://www.cnblogs.com/lin-bear/p/5193096.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值