关于spring的文件上传

本文介绍了一个使用Spring框架实现文件上传的方法。具体展示了如何通过MultipartHttpServletRequest处理文件上传请求,并将文件保存到服务器指定目录的过程。
文件上传有很多方式,每个框架都有自己的一套机制,spring的文件上传代码如下:
/**
  * 将license文件上传到服务器端
  * 将客户端(IE)中选择的license文件上载到服务器端的临时目录中,
  * 返回上载后的文件对象
  * @param request HttpServletRequest
  * @param filename IE中的file组件的名称
  * @return java.io.File
  * @throws Exception
  */
 public File CreateLicenseFile(HttpServletRequest request,String filename) throws Exception{
  File uploadedFile = null;
  if (request instanceof MultipartHttpServletRequest) {
   MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
         MultipartFile multipartFile = multipartRequest.getFile(filename);
         String uploadDir = BeanContextAccessor.getCommonParameterManager().getCommonParameter(
           ConstantsBpm2FileManager.FILE_UPLOAD_PATH,
           ConstantsBpm2License.SYSTEM_PARAMETER).getValue();;
         File dirPath = new File(uploadDir);
         if (!dirPath.exists()) {
             dirPath.mkdirs();
         }
   if (!multipartFile.getOriginalFilename().equals("")) {
    String sep = System.getProperty("file.separator");
    uploadedFile = new File(uploadDir +
          sep+
          multipartFile.getOriginalFilename());
    multipartFile.transferTo(uploadedFile);
   }
  }
  return uploadedFile;
 }
其中:filename是前台的类型为file的input的name.
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值