springmvc上传下载

博客给出了文件上传与下载的代码实现。上传部分通过Spring的@RequestMapping注解处理,可实现多文件上传,会判断文件是否为空并处理上传结果。下载部分同样使用@RequestMapping注解,对文件名进行编码防止乱码,设置响应头后返回文件字节数组。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

上传:

@RequestMapping("/upload")
    public String upload(Model m,String name,List<MultipartFile> uploadFile,HttpServletRequest request){
        if(!uploadFile.isEmpty()&&uploadFile.size()>0){
            String path=request.getServletContext().getRealPath("/upload/");
            File file=new File(path);
            if(!file.exists()){
                file.mkdirs();
            }
            for(MultipartFile item:uploadFile){
                String oldName=item.getOriginalFilename();
                String newFileName=name+"_"+UUID.randomUUID()+"_"+oldName;
                
                try {
                    item.transferTo(new File(path,newFileName));
                    m.addAttribute("msg", "上传成功");
                } catch (Exception e) {
                    e.printStackTrace();
                    m.addAttribute("msg", "上传错误");
                }
            }
        }else{
            m.addAttribute("msg", "无上传文件");
        }
        return "success";
    }

//multiple="multiple"该属性是h5新特性,指定后可以进行多文件上传

<form action="${pageContext.request.contextPath }/upload" enctype="multipart/form-data" method="post">
        <input type="text" name="name" /><br>
        <input type="file" name="uploadFile" multiple="multiple" /> <br/>
        <input type="submit" value="提交" />
    </form>

 

下载:

@RequestMapping("/download")
    public ResponseEntity<byte[]> download(HttpServletRequest request,String fileName) throws Exception{
        //fileName=new String(fileName.getBytes("ISO-8859-1"),"UTF-8");
        String path=request.getServletContext().getRealPath("/upload/");
        //创建文件对象
        File file=new File(path+File.separator+fileName);
        
        fileName=getFileName(request, fileName);
        //设置响应头
        HttpHeaders hreaders=new HttpHeaders();
        
        hreaders.setContentDispositionFormData("attachment", fileName);
        
        hreaders.setContentType(MediaType.APPLICATION_OCTET_STREAM);
        
        return new ResponseEntity<byte[]>(FileUtils.readFileToByteArray(file),hreaders,HttpStatus.OK);
    }

 

/**
     * 对文件名进行编码  防止乱码
     * @param request
     * @param fileName
     * @return
     * @throws Exception 
     */
    public String getFileName(HttpServletRequest request,String fileName) throws Exception{
        String[] IEkey={"MSIE","Trident","Edge"};
        //获取请求头代理信息
        String userAgent=request.getHeader("User-Agent");
        
        for(String keyWord:IEkey){
            return URLEncoder.encode(fileName, "utf-8");
        }
        return new String(fileName.getBytes("UTF-8"),"ISO-8859-1");
    }

 

 <a href="${pageContext.request.contextPath }/download?fileName=<%=URLEncoder.encode("符越涵_7f34c071-9968-4be0-848e-a136378359ba_捕获.PNG", "utf-8") %>">
        下载文件
    </a>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值