且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Spring MVC中的文件上传和下载

更新时间:2021-08-08 19:57:21

文件上传使用了apache commons fileupload 和commons IO,

在使用之前,相关jar包需要导入项目工程目录。


uploadForm.jsp


<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>

<!DOCTYPE html PUBLIC "-//W3C/DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd" >
<html>
<head>
	<meta content="text/html; charset=UTF-8" http-equiv="Content-Type">
	<title>文件上传</title>
</head>
<body>

<h2>文件上传</h2>
<br>
<form:form action="upload" enctype="multipart/form-data" method="post">
	<table>
		<tr>
			<td><label>用户名: </label></td>
			<td><input type="text" name="username"></td>

		</tr>
		<tr>
			<td><label>请上传头像: </label></td>
			<td><input type="file" name="image"></td>

		</tr>
		<tr>
			<td><input id="submit" type="submit" value="upload"></td>
		</tr>
	
	</table>

</form:form>

</body>
</html>

FileUploadController.java


package org.fkit.controller;

import java.io.File;


import javax.servlet.http.HttpServletRequest;

import org.apache.commons.io.FileUtils;
import org.fkit.domain.User;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;



@Controller
public class FileUploadController {
	
	@RequestMapping(value="/upload", method=RequestMethod.POST)
	public String upload(HttpServletRequest request,
			@ModelAttribute User user,
			Model model) throws Exception {
		System.out.println(user.getUsername());
		if (!user.getImage().isEmpty()) {
			//String path = "D:\\tmp\\";
			String path = request.getServletContext().getRealPath("/images/");
			String filename = user.getImage().getOriginalFilename();
			File filepath = new File(path, filename);
			if (!filepath.getParentFile().exists()) {
				filepath.getParentFile().mkdirs();
			}
			user.getImage().transferTo(new File(path+File.separator + filename));
			model.addAttribute("user", user);
			return "success";
		} else {
			return "error";
		}
		
	}
	
	@RequestMapping(value="/download")
	public ResponseEntity<byte[]> download(HttpServletRequest request,
			@RequestParam("filename") String filename,
			Model model) throws Exception{
		String path = request.getServletContext().getRealPath("/images/");
		File file = new File(path + File.separator + filename);
		HttpHeaders headers = new HttpHeaders();
		String downloadFileName = new String(filename.getBytes("UTF-8"), "iso-8859-1");
		headers.setContentDispositionFormData("attachment",  downloadFileName);
		headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
		return new ResponseEntity<byte[]>(FileUtils.readFileToByteArray(file),
				headers, HttpStatus.CREATED);
		
				
	}

}

Spring MVC中的文件上传和下载

Spring MVC中的文件上传和下载