JAVA Spring web mvc 学习 之 7:文件上\下传

本文介绍如何在Spring MVC框架中实现文件的上传与下载功能。主要包括使用MultipartFile类处理文件上传,设置文件上传控制器,并在前端通过form表单进行文件上传操作;同时介绍了文件下载的实现方式,包括使用HttpHeaders等类来处理文件下载。

文件上传:---------------------

1.控制器添加文件上传处理控制器,使用org.springframework.web.multipart.MultipartFile 类处理文件

// 上传文件会自动绑定到MultipartFile中

@RequestMapping(value="/upload",method=RequestMethod.POST)
public String upload(HttpServletRequest request,
@RequestParam("description") String description,
@RequestParam("file") MultipartFile file) throws Exception{
 
   System.out.println(description);
   // 如果文件不为空,写入上传路径
if(!file.isEmpty()){
// 上传文件路径
String path = request.getServletContext().getRealPath(
               "/images/");
// 上传文件名
String filename = file.getOriginalFilename();
   File filepath = new File(path,filename);
// 判断路径是否存在,如果不存在就创建一个
       if (!filepath.getParentFile().exists()) { 
        filepath.getParentFile().mkdirs();
       }
       // 将上传文件保存到一个目标文件当中
file.transferTo(new File(path+File.separator+ filename));
return "success";
}else{
return "error";
}
 

}

2.前台采用form,定义文件上传

<form action="upload" enctype="multipart/form-data" method="post">
<table>
<tr>
<td>文件描述:</td>
<td><input type="text" name="description"></td>
</tr>
<tr>
<td>请选择文件:</td>
<td><input type="file" name="file"></td>
</tr>
<tr>
<td><input type="submit" value="上传"></td>
</tr>
</table>
</form>

3.可在spring配置文件中,增加文件上传控制器。

   <bean id="multipartResolver"  
        class="org.springframework.web.multipart.commons.CommonsMultipartResolver">  
<!-- 上传文件大小上限,单位为字节(10MB) -->
        <property name="maxUploadSize">  
            <value>10485760</value>  
        </property>  
        <!-- 请求的编码格式,必须和jSP的pageEncoding属性一致,以便正确读取表单的内容,默认为ISO-8859-1 -->
        <property name="defaultEncoding">
        <value>UTF-8</value>
        </property>
    </bean>

----------------------------------------

文件下载:

控制器中加入下载处理,使用 org.springframework.http 包下HttpHeaders\HttpStatus\MediaType\ResponseEntity 相关类

@RequestMapping(value="/download")
public ResponseEntity<byte[]> download(HttpServletRequest request,
@RequestParam("filename") String filename,
Model model)throws Exception{
// 下载文件路径
String path = request.getServletContext().getRealPath(
                "/images/");
File file = new File(path+File.separator+ filename);
        HttpHeaders headers = new HttpHeaders();  
        // 下载显示的文件名,解决中文名称乱码问题  
        String downloadFielName = new String(filename.getBytes("UTF-8"),"iso-8859-1");
        // 通知浏览器以attachment(下载方式)打开图片
        headers.setContentDispositionFormData("attachment", downloadFielName); 
        // application/octet-stream : 二进制流数据(最常见的文件下载)。
        headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
        // 201 HttpStatus.CREATED
        return new ResponseEntity<byte[]>(FileUtils.readFileToByteArray(file),    
                headers, HttpStatus.CREATED);  
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值