您的位置:首页 > 编程语言 > Java开发

Spring mvc文件上传与下载

2017-06-01 18:53 363 查看
package cn.happyangel2008.controller;

import java.io.BufferedInputStream;

import java.io.BufferedOutputStream;

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 java.text.SimpleDateFormat;

import javax.annotation.Resource;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

import org.apache.commons.fileupload.FileUpload;

import org.apache.commons.io.FileUtils;

import org.springframework.stereotype.Controller;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestParam;

import org.springframework.web.multipart.MultipartFile;

import org.springframework.web.servlet.ModelAndView;

@Controller

public class FileController {

/*

* SpringMVC中的文件上传

* @第一步:由于SpringMVC使用的是commons-fileupload实现,故将其组件引入项目中

* @这里用到的是commons-fileupload-1.2.1.jar和commons-io-1.3.2.jar

* @第二步:spring-mvx中配置MultipartResolver处理器。可在此加入对上传文件的属性限制

* <bean id="multipartResolver"

* class="org.springframework.web.multipart.commons.CommonsMultipartResolver">

* <!-- 设置上传文件的最大尺寸为10MB -->

* <property name="maxUploadSize">

* <value>10000000</value>

* </property>

* </bean>

* 第三步:在Controller的方法中添加MultipartFile参数。该参数用于接收表单中file组件的内容

*第四步:编写前台表单。注意enctype="multipart/form-data"以及<input type="file" name="****"/>

* 如果是单个文件 直接使用MultipartFile 即可

*/

@RequestMapping("/upload.html")

public ModelAndView upload(String name,

//上传多个文件

@RequestParam("file") MultipartFile[] file,

HttpServletRequest request) throws IllegalStateException,

IOException {

System.out.println("222");

//获取文件 存储位置

String realPath = request.getSession().getServletContext()

.getRealPath("/uploadFile");

File pathFile = new File(realPath);

System.out.println("路径"+realPath);

if (!pathFile.exists()) {

//文件夹不存 创建文件

pathFile.mkdirs();

}

for (MultipartFile f : file) {

System.out.println("文件类型:"+f.getContentType());

System.out.println("文件名称:"+f.getOriginalFilename());

System.out.println("文件大小:"+f.getSize());

System.out.println(".................................................");

//将文件copy上传到服务器

f.transferTo(new File(realPath + "/" + f.getOriginalFilename()));

//FileUtils.copy

}

//获取modelandview对象

ModelAndView view = new ModelAndView();

view.setViewName("redirect:index.jsp");

return view;

}

@RequestMapping("/download.html")

public String download(String fileName, HttpServletRequest request,

HttpServletResponse response) {

response.setCharacterEncoding("utf-8");

response.setContentType("multipart/form-data");

response.setHeader("Content-Disposition", "attachment;fileName="

+ fileName);

try {

String path = request.getSession().getServletContext().getRealPath

("uploadFile")+File.separator;

InputStream inputStream = new FileInputStream(new File(path

+ fileName));

OutputStream os = response.getOutputStream();

byte[] b = new byte[2048];

int length;

while ((length = inputStream.read(b)) > 0) {

os.write(b, 0, length);

}

// 这里主要关闭。

os.close();

inputStream.close();

} catch (FileNotFoundException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

}

// 返回值要注意,要不然就出现下面这句错误!

//java+getOutputStream() has already been called for this response

return null;

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息