Spring 实现文件上传与下载
来源:互联网 发布:ubuntu安装openjdk1.8 编辑:程序博客网 时间:2024/06/06 01:27
文件上传
upload.jsp:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ include file="/taglibs.jsp"%><!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=UTF-8"><title>Insert title here</title></head><body> <h2 > 文件上传 </h2> <div class="pageContent"> <form method="post" action="/dhcss/ssp/upload/upload.do" enctype="multipart/form-data"> <div class="pageFormContent nowrap" layoutH="97"> <input type="hidden" name="name" /> <input type="file" name="file"/><div class="divider"></div><p>自定义扩展请参照:dwz.validate.method.js</p><p>错误提示信息国际化请参照:dwz.regional.zh.js</p></div><div class="formBar"><ul><li><div class="buttonActive"><div class="buttonContent"><button type="submit">提交</button></div></div></li><li><div class="button"><div class="buttonContent"><button type="button" class="close">取消</button></div></div></li></ul></div> </form> </div></body></html>
java 上传代码;
package com.dhcc.dhcss.ssp.sm;import java.io.File;import java.util.Date;import javax.servlet.ServletContext;import javax.servlet.http.HttpServletRequest;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestMethod;import org.springframework.web.bind.annotation.RequestParam;import org.springframework.web.context.ServletContextAware;import org.springframework.web.multipart.commons.CommonsMultipartFile;@Controller //声明该类为控制器类@RequestMapping("/ssp/upload")public class FileUploadController implements ServletContextAware{ //实现ServletContextAware接口,获取本地路径 private ServletContext servletContext; public void setServletContext(ServletContext servletContext) { //实现接口中的setServletContext方法 this.servletContext = servletContext; } @RequestMapping(value = "/upload", method = RequestMethod.POST) //将文件上传请求映射到该方法 public String handleFormUpload(@RequestParam("name") String name, //设置请求参数的名称和类型 @RequestParam("file") CommonsMultipartFile mFile,HttpServletRequest req) { //请求参数一定要与form中的参数名对应 if (!mFile.isEmpty()) { //String path = this.servletContext.getRealPath("/"); //获取本地存储路径// String path="/home/chen/"; String pc=req.getServletContext().getRealPath("/WEB-INF/conf.properties"); Configuration rc = new Configuration(pc);//相对路径 String path=rc.getValue("path"); System.out.println(path); //File file = new File(path + new Date().getTime() + ".jpg"); //新建一个文件 File file = new File(path + new Date().getTime()); //新建一个文件 System.out.println(file.getAbsolutePath()); try { mFile.getFileItem().write(file); //将上传的文件写入新建的文件中 } catch (Exception e) { e.printStackTrace(); } return "ssp/sm/uploadSuccess"; //返回成功视图 }else { return "ssp/sm/uploadFail"; //返回失败视图 } }public void setServletContext1(ServletContext arg0) {// TODO Auto-generated method stub}}
uploadSuccess.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=UTF-8"><title>Insert title here</title></head><body><h3 align='center'><font color='red'>上传成功,请去主文件夹下查看~~</font></h3><a href="http://localhost:8080/dhcss">返回首页</a></body></html>uploadFail.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=UTF-8"><title>Insert title here</title></head><body><h3 align='center'><font color='red'>上传失败~~</font></h3><a href="http://localhost:8080/dhcss">返回首页</a></body></html>-------------------------------------------------------------------------------------------
download.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=UTF-8"><script type="text/javascript" src="../../js/jquery-1.7.1.js"></script><title>upload</title></head><body><h2 > 文件下载 </h2> <div> <div class="pageFormContent nowrap" layoutH="97">请输入文件夹下要下载的文件名(限英文名)<input id='fileName' type="text" name="fileName" /><div class="divider"></div><p>自定义扩展请参照:dwz.validate.method.js</p><p>错误提示信息国际化请参照:dwz.regional.zh.js</p></div><div class="formBar"><ul><li><div class="buttonActive"><div class="buttonContent"><a id="btn" href="/dhcss/download.do" target="blank"><button>下载</button></a></div></div></li><li><div class="button"><div class="buttonContent"><button type="button" class="close">取消</button></div></div></li></ul></div></div></body><script type="text/javascript">$(function(){ $('a').click(function(){ var link=this.href+'?'+'fileName='+$('#fileName').val(); window.open(link); return false; }); }); </script></html>
java 文件下载代码:
package com.dhcc.dhcss.ssp.sm;import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; @Controller public class FileDownloadController { //static final String dir = "/home/chen/"; @RequestMapping("download.do") public void downloadFile(@RequestParam String fileName,HttpServletResponse response,HttpServletRequest req){ response.setCharacterEncoding("utf-8"); response.setContentType("multipart/form-data"); response.setHeader("Content-Disposition", "attachment;fileName="+fileName); String pc=req.getServletContext().getRealPath("/WEB-INF/conf.properties"); Configuration rc = new Configuration(pc);//相对路径 String dir=rc.getValue("path"); try { File file=new File(dir+fileName); System.out.println(file.getAbsolutePath()); InputStream inputStream=new FileInputStream(file); OutputStream os=response.getOutputStream(); byte[] b=new byte[1024]; int length; while((length=inputStream.read(b))>0){ os.write(b,0,length); } inputStream.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }
- Spring 实现文件上传与下载
- Spring MVC 实现文件上传与下载
- spring+mongodb 文件上传与下载实现
- spring文件上传与下载
- 利用spring 实现文件上传、下载
- spring mvc文件上传与下载
- Spring mvc 文件的上传与下载
- Spring MVC之文件上传与下载
- Spring MVC 文件上传与下载
- Spring MVC 文件上传与下载
- Spring mvc文件上传与下载
- spring 文件上传与下载【idea】
- Spring Boot文件上传与下载
- spring boot(23)-文件上传与下载
- struts 实现文件上传与下载
- struts 实现文件上传与下载
- struts 实现文件上传与下载
- struts 实现文件上传与下载
- 纯虚函数及抽象类
- 硬件系统设计:该用FPGA来做呢还是用单片机?
- C#winform 打印图像,PrintDialog打印并且设置打印纸张大小
- JSP的四个作用域
- 发送http请求
- Spring 实现文件上传与下载
- poj1730
- 手动插int 3实现程序主动断点
- C setjmp和longjmp
- DLL:VS2005使用C++制作dll文件
- 世界最小的高清玩具单反数码相机
- firefox event.srcElement
- NeatUpload的web.config配置
- 使用Win32API实现Windows下异步串口通讯(上)