1. 程式人生 > >SpringMVC實現上傳

SpringMVC實現上傳

1.建立/jq_springmvc/WebContent/WEB-INF/jsp/upload.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="content-type" content="text/html;chrset=UTF-8">
<link rel="stylesheet" type="text/css" href="css/first.css">
</head>
<body>
<h2>上傳檔案</h2>
<form action="

upload.action" method="post" enctype="multipart/form-data">
<input type="file" name="file"><br>
<input type="file" name="file"><br>
<input type="submit" name="上傳"><br>
</form>
</body>

</html>

2.匯入jar包(不用build path,直接複製貼上到lib下)


3.在/jq_springmvc/src/SpringMVC-servlet.xml配置檔案中配置(紅字部分是寫死的不用改動,只需要改檔案大小

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
        http://www.springframework.org/schema/context 
http://www.springframework.org/schema/context/spring-context-3.2.xsd">

<bean 

            id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">

                    <property name="defaultEncoding" value="UTF-8"></property>

<property name="maxUploadSize" value="200000000"></property>
</bean>


<!--檢視解析器 -->
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass"
value="org.springframework.web.servlet.view.JstlView"></property>
<property name="prefix" value="/WEB-INF/jsp/"></property>
<property name="suffix" value=".jsp"></property>
</bean>

</beans>

4.建立一個實現類


@Controller

public class UpAndDownController {

//上傳
@RequestMapping("/up.action")
public String up() {
return "upload";
}


@RequestMapping("/upload.action")
public String upload(@RequestParam("file") MultipartFile[] files,
HttpServletRequest request) {
if (files != null && files.length > 0) {
for (MultipartFile file : files) {
// 上傳檔案
saveFile(request, file);
}
}
return "index";
}


private voidsaveFile(HttpServletRequest req, MultipartFile file) {
try {
// 判斷檔案是否是空
if (!file.isEmpty()) {
// 獲取檔案上傳到的位置
String filepath = req.getSession().getServletContext()
.getRealPath("/")
+ "upload/" + file.getOriginalFilename();

File newFile = new File(filepath);
if (!newFile.getParentFile().exists()) {
newFile.getParentFile().mkdirs();
}
// 上傳檔案
file.transferTo(newFile);
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

5.訪問:http://localhost:8888/jq_springmvc/upload.action
6.總結

上傳
1)頁面
<form action="upload.action" method="post" enctype="multipart/form-data">
2)jar包
3)springMVC 配置檔案中 
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="defaultEncoding" value="UTF-8"></property>
<property name="maxUploadSize" value="200000000"></property>
</bean>
4)up
path
// 獲取檔案上傳到的位置
String filepath = req.getSession().getServletContext()
.getRealPath("/")

+ "upload/" + file.getOriginalFilename();//上傳檔名

7.jar包

連結:https://pan.baidu.com/s/1YATmieYahxB2PBx3NYXMjA 密碼:blxq