login.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<jsp:useBean id="user" class="com.sjxy.ssm.entity.User"></jsp:useBean>
<!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>
<center>
<a href="${pageContext.request.contextPath}/emp/list">显示所有员工信息</a>
<form:form modelAttribute="user" action="${pageContext.request.contextPath}/user/upload" enctype="multipart/form-data" method="post">
<table>
<tr>
<td>Email address</td>
<td><input name="email"/></td>
<td><form:errors path="email" cssStyle="color:red"/></td>
</tr>
<tr>
<td>Password</td>
<td><input type="password" name="password"/></td>
<td><form:errors path="password" cssStyle="color:red"/></td>
<tr>
<td>File input <input type="file" name="image" ></td>
<td><input type="checkbox">
Check me out</td>
<td><button type="submit">提交</button></td>
</tr>
</table>
</form:form>
</center>
</body>
</html>
首先导入useBean
<jsp:useBean id="user" class="com.sjxy.ssm.entity.User"></jsp:useBean>
数据绑定,modelAttribute=”user”最好要写
<form:form modelAttribute="user" action="${pageContext.request.contextPath}/user/upload" enctype="multipart/form-data" method="post">
<td>Email address</td>
<td><input name="email"/></td>
<td><form:errors path="email" cssStyle="color:red"/></td>
Controller做的上传下载register.java
package com.sjxy.ssm.web.hander
import java.io.File
import java.io.IOException
import java.io.UnsupportedEncodingException
import javax.servlet.http.HttpServletRequest
import javax.validation.Valid
import org.apache.commons.io.FileUtils
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.validation.Errors
import org.springframework.web.bind.annotation.ModelAttribute
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RequestParam
import com.sjxy.ssm.entity.User
@Controller
@RequestMapping(value="/user")
public class register {
@RequestMapping(value="/upload")
public String upload(@Valid @ModelAttribute("user") User user,Errors errors,
Model model,HttpServletRequest request) throws Exception {
System.out.println(user.getEmail())
//logger.info(user)
if(errors.hasErrors()){
return "user/login"
}
if(!user.getImage().isEmpty()){
String path="E:/upload"
System.out.println(path)
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 "user/success"
}
return "user/error"
}
@RequestMapping(value="/download")
public ResponseEntity<byte[]> download(HttpServletRequest request,
@RequestParam("filename") String filename,Model medel) throws Exception{
String path="E:/upload"
System.out.println(filename)
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)
}
}