Spring轻松读取文件方法

<!--自己实现上传逻辑,用来返回进度条-->
    <bean id="multipartResolver" 
        class="com.jssh.credit.infrastructure.multipart.resolver.CommonsMultipartResolverExt">
        <!-- 设置上传文件的最大尺寸为10MB 10*1024*1024 -->
        <property name="maxUploadSize">
            <value>524288000</value>
        </property>
        <property name="defaultEncoding">
            <value>UTF-8</value>
        </property>
    </bean>
@RequestMapping("/addBrokersByFile")
    @ResponseBody
    public ResponseMessage addBrokersByFile(HttpServletRequest request, MultipartHttpServletRequest multipartRequest){

        UserRequestDTO user = WebHelper.getUser(request);
        if (user==null)
            return new ResponseMessage(ResponseConstants.登录失效,"登录已失效");


        // 先实例化一个文件解析器
        CommonsMultipartResolver commonsMultipartResolver = new CommonsMultipartResolver(request.getSession()
                .getServletContext());

        // 判断request请求中是否有文件上传
        if (commonsMultipartResolver.isMultipart(request)) {

            MultipartFile multipartFile = multipartRequest.getFile("file");//文件

            String _fileName = multipartFile.getOriginalFilename();//原始文件名
            String suffix = _fileName.substring(_fileName.lastIndexOf("."));//后缀名

            //判断文件的格式
            if (".xls".equals(suffix)){
                String localPath = MyConfigProperty.getProperty("uploads_local_path");
                if (StringUtils.isNotBlank(localPath)&&localPath.startsWith("file:"))
                    localPath = localPath.substring(5);
                String fileName = localPath+"\\excel\\"+user.getUserAccount()+"\\"+ DateUtils.dfYMDHM.format(new Date())+"\\";

                //上传后记录的文件...
                File excelFile = new File(fileName+_fileName);
                if (!excelFile.exists()){
                    new File(fileName).mkdirs();
                    try {
                        excelFile.createNewFile();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                try {
                    multipartFile.transferTo(excelFile);
                } catch (IOException e) {
                    e.printStackTrace();
                    return new ResponseMessage(ResponseConstants.ERROR,"文件解析失败");
                }

                try{

                    return new ResponseMessage(ResponseConstants.OK,"Excel文件导入成功");
                }catch(Exception e){
                    return new ResponseMessage(ResponseConstants.ERROR,e.getMessage());
                }




            }else{
                return new ResponseMessage(ResponseConstants.ERROR,"文件格式不正确,请使用下载的模版填写");
            }
        }
        return null;
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值