Spring 实现文件上传与下载

文件上传

upload.jsp:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
 <%@ include file="/taglibs.jsp"%>




Insert title here



   

文件上传

自定义扩展请参照:dwz.validate.method.js

错误提示信息国际化请参照:dwz.regional.zh.js


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"%>




Insert title here


上传成功,请去主文件夹下查看~~

返回首页



 
  

 
  
uploadFail.jsp: 

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>




Insert title here


上传失败~~

返回首页


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

download.jsp


<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>





upload


文件下载

请输入文件夹下要下载的文件名(限英文名)

自定义扩展请参照:dwz.validate.method.js

错误提示信息国际化请参照:dwz.regional.zh.js


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();  
        }  
    }  
}  



你可能感兴趣的:(Java)