springMVC+commons文件的上传与下载

web.xml


  
    suibian
    org.springframework.web.servlet.DispatcherServlet
    
      contextConfigLocation
      classpath:springmvc.xml
    
    1
  
  
    suibian
    /
  


文件下载:

FileDownController.java

package com.chenk.web.controller;

import java.io.File;
import java.io.IOException;
import java.io.OutputStream;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.io.FileUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class FileDownController {
	@RequestMapping("/fileDown")
	public void fileDown(String filename, HttpServletResponse response, HttpServletRequest request) throws IOException {
		// 1、在相应的头添加相关信息。防止浏览器对文件打开,下载的文件名
		// 一定要理解addHeader和setContentType的区别
		// setContentType是设置相应的类型,修改相应的数据包中的已有的头信息
		// addHeader表示在响应的头中,添加信息的响应信息,如:告知浏览器如下响应的内容。禁止打开
		response.addHeader("Content-disposition", "attachment;filename=" + filename);
		// 2、读取需要下载的文件,做路径转换
		ServletContext sc = request.getServletContext();
		String realPath = sc.getRealPath("/file/" + filename);
		System.out.println(realPath);
		//FileUtils.readFileToByteArray commons-io.jar 
		byte[] arr = FileUtils.readFileToByteArray(new File(realPath));
		// 3、将文件响应给客户端浏览器
		OutputStream os = response.getOutputStream();
		os.write(arr);
		os.flush();
		// 4、关闭流
		os.close();
	}
}

springmvc.xml



	
	
	
	
	
	
	
	
	
	
	
	
 


文件上传:

package com.chenk.web.controller;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

import javax.servlet.ServletContext;
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;
import org.springframework.web.multipart.commons.CommonsMultipartFile;
/**
 * 文件的上传
 */
@Controller
public class FileUPloadController {
	@RequestMapping("/fileUpload")
public String fileUpload(String fileDesc,@RequestParam("filename")CommonsMultipartFile file,HttpServletRequest request,HttpServletResponse response) throws IOException{
	System.out.println(fileDesc);
	String filename=file.getOriginalFilename();
	String newFileName=UUID.randomUUID().toString()+filename.substring(filename.lastIndexOf("."));
	
/*	1、最原始的做法:
 * InputStream is=file.getInputStream();
	byte[] arr=new byte[is.available()];
	is.read(arr);
	ServletContext sc=request.getServletContext();
	String name=sc.getRealPath("/file/"+newFileName);
	System.out.println(name);
	OutputStream os=new FileOutputStream(name);
	os.write(arr);
	os.flush();
	os.close();
	is.close();*/
//	2、使用fileupload组件
	ServletContext sc=request.getServletContext();
	String name=sc.getRealPath("/file/"+newFileName);
	file.transferTo(new File(name));
	return "ok";
	}
}

springmvc.xml


	
	
	
	
	
	
	
	
	
	
	
	


 
 
 
 
 
 
 
 



你可能感兴趣的:(常用框架)