SpringMVC上传和下载

搭建SpringMvc开发环境

导入包:

commons-fileupload-1.2.1.jar

commons-io-2.0.jar

配置文件上传解析器

 <bean id="multipartResolver"
        class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        <!--上传文件的最大大小>
        <property name="maxUploadSize" value="17367648787"></property>
<!-- 上传文件的编码 --> 
       <property name="defaultEncoding" value="UTF-8"></property> 
   </bean>

上传页面

<a href="down">下载图片</a>
    
    <form action="up" method="post" enctype="multipart/form-data">
        头像:<input type="file" name="uploadFile" />
        描述:<input type="text" name="desc" />
        <input type="submit" value="上传" />
    </form>

控制器方法

@Controller
public class TestUploadAndDownController {

    @RequestMapping("/down")
    public ResponseEntity<byte[]> down(HttpSession session) throws IOException{
        
        //获取下载文件的路径
        String realPath = session.getServletContext().getRealPath("img");
        String finalPath = realPath + File.separator + "2.jpg";
        InputStream is = new FileInputStream(finalPath);
        //available():获取输入流所读取的文件的最大字节数
        byte[] b = new byte[is.available()];
        is.read(b);
        //设置请求头
        HttpHeaders headers = new  HttpHeaders();
        headers.add("Content-Disposition", "attachment;filename=zzz.jpg");
        //设置响应状态
        HttpStatus statusCode = HttpStatus.OK;
        ResponseEntity<byte[]> entity = new ResponseEntity<byte[]>(b, headers, statusCode);
        return entity;
    }
    
    @RequestMapping(value="/up", method=RequestMethod.POST)
    public String up(String desc, MultipartFile uploadFile, HttpSession session) throws IOException {
        //获取上传文件的名称
        String fileName = uploadFile.getOriginalFilename();
        String finalFileName = UUID.randomUUID() + fileName.substring(fileName.lastIndexOf("."));
        String path = session.getServletContext().getRealPath("photo") + File.separator + finalFileName;
        File file = new File(path);
        uploadFile.transferTo(file);
        return "success";
    }
    
    @RequestMapping(value="/up_old", method=RequestMethod.POST)
    public String up_old(String desc, MultipartFile uploadFile, HttpSession session) throws IOException {
        //获取上传文件的名称
        String fileName = uploadFile.getOriginalFilename();
        String path = session.getServletContext().getRealPath("photo") + File.separator + fileName;
        //获取输入流
        InputStream is = uploadFile.getInputStream();
        //获取输出流
        File file = new File(path);
        OutputStream os = new FileOutputStream(file);
        /*int i = 0;
        while((i = is.read()) != -1) {
            os.write(i);
        }*/
        
        /*int i = 0;
        byte[] b = new byte[1024];
        while((i = is.read(b)) != -1) {
            os.write(b, 0, i);
        }*/
        
        os.close();
        is.close();
        return "success";
    }
    
}

转载于:https://my.oschina.net/u/4151492/blog/3064459

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值