Spring的上传与下载

1、话不多说,先贴代码

package com.qf.file.controll;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.io.FileUtils;
import org.apache.tools.ant.taskdefs.email.Header;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.context.ContextLoaderListener;
import org.springframework.web.multipart.MultipartFile;

@Controller
public class FileControll {
	@RequestMapping("/fileup")
	public String fileUp(@RequestParam(name="file",required=true)MultipartFile file,HttpServletRequest request) throws IllegalStateException, IOException{
		//获取文件的名字
		String fileName=file.getOriginalFilename();
		//接下来:构建上传数据的路径
		String path=request.getRealPath("/img"+File.separator+fileName);
		//接下来保存数据
		file.transferTo(new File(path));
		return "/index.jsp";
	}
	
	@RequestMapping("/filedown")
	public ResponseEntity<byte[]> fileDown(HttpServletRequest request) throws Exception{
		//获取下载的文件路径
		String path=request.getRealPath("/img/aaa.jpg");
		//设置请求的头
		HttpHeaders headers=new HttpHeaders();
		headers.add("content-disposition","attachment;filename=aaa.jpg");
		//设置返回数据的状态
		HttpStatus status=HttpStatus.OK;
		return new ResponseEntity<byte[]>(FileUtils.readFileToByteArray(new File(path)), headers, status);
	}	
	
}

2、前端代码

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>My JSP 'index.jsp' starting page</title>
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
  </head>
  
  <body>
		<form action="${pageContext.request.contextPath}/fileup.action" method="post" enctype="multipart/form-data">
		    <input type="file" name="file" id="file" value="" /><br />
			<input type="submit" value="上传"/>
		</form>
  </body>
</html>

3、导包 

<dependency>
	<groupId>log4j</groupId>
	<artifactId>log4j</artifactId>
	<version>1.2.17</version>
</dependency>
<dependency>
	<groupId>commons-logging</groupId>
	<artifactId>commons-logging</artifactId>
	<version>1.2</version>
</dependency>

4、SpringMVC配置 

<!--让当前的SpringMVC支持文件的上传-->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
       <!--控制上传文件的大小-->
       <property name="maxUploadSize" value="23376434"></property>
       <!--上传文件的数据编码-->
       <property name="defaultEncoding" value="UTF-8"></property>
</bean>

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值