maven项目SSM上传下载

2 篇文章 0 订阅
2 篇文章 0 订阅

需要jar包

 <!-- 文件上传 -->  
		<dependency>  
			<groupId>commons-fileupload</groupId>  
			<artifactId>commons-fileupload</artifactId>  
			<version>1.3</version>  
		</dependency>  

jsp页面

<%@ page language="java" contentType="text/html; charset=utf-8"  
    pageEncoding="utf-8"%>  
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">  
<html>  
<head>  
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">  
<title>文件上传下载</title>  
</head>  
<body>  
<form method="post" action="<%=request.getContextPath() %>/doUpload.do" enctype="multipart/form-data">  
<input type="file" name="file"/>  
<button type="submit" >提交</button>  
</form> 
    <hr>  
    <form action="<%=request.getContextPath() %>/down.do" method="get">  
        <input type="submit" value="下载">  
    </form>  
</body>  
</html>

在springmvc文件设置

	<!-- 定义文件解释器 -->  
	<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">    
	    <!-- 设置默认编码 -->  
	    <property name="defaultEncoding" value="utf-8"></property>  
	    <!-- 上传图片最大大小10M-->   
	    <property name="maxUploadSize" value="10484880"></property>    
	</bean>  

在Controller层添加方法

/** 
     * 上传单个文件操作 
     * @param RequestParam 括号中的参数名file和表单的input节点的name属性值一致 
     * @return 
     */  
    @RequestMapping(value="doUpload", method=RequestMethod.POST)  
    public void doUploadFile(@RequestParam("file") MultipartFile file){  
        if(!file.isEmpty()){  
            try {  
                //这里将上传得到的文件保存目录  
                FileUtils.copyInputStreamToFile(file.getInputStream(), new File("E:\\temp",   
                		System.currentTimeMillis()+ file.getOriginalFilename()));  
            } catch (IOException e) {  
                e.printStackTrace();  
            }  
        }  
  
    }  
      
    /**  
     * 文件下载功能  
     * @param request  
     * @param response  
     * @throws Exception  
     */  
    @RequestMapping("down")  
    public void down(HttpServletRequest request,HttpServletResponse response) throws Exception{  
        //下载路径+文件
        String fileName = "E:\\FileDownLoad.zip";  
        //获取输入流  
        InputStream bis = new BufferedInputStream(new FileInputStream(new File(fileName)));  
        //假如以中文名下载的话   重命名下载后的名字 
        String filename = "下载文件.zip";  
        //转码,免得文件名中文乱码  
        filename = URLEncoder.encode(filename,"UTF-8");  
        //设置文件下载头  
        response.addHeader("Content-Disposition", "attachment;filename=" + filename);    
        //1.设置文件ContentType类型,这样设置,会自动判断下载文件类型    
        response.setContentType("multipart/form-data");   
        BufferedOutputStream out = new BufferedOutputStream(response.getOutputStream());  
        int len = 0;  
        while((len = bis.read()) != -1){  
            out.write(len);  
            out.flush();  
        }  
        out.close();  
    }  

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值