圖片上傳/檔案上傳
1. 建立一個web項目 建立一個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>
</head>
<body>
<form action="<%=request.getContextPath()%>/upload.action" method="post" enctype="multipart/form-data">
請選擇上傳的檔案/圖片:
<input type="file" name="file"/>
<!-- 請選擇上傳的圖片: -->
<!-- <input type="file" name="file" accept="image/*" /> -->
<input type="submit" value="上傳"/>
</form>
</body>
</html>
2 web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<display-name></display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>SpringMVC</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:SpringMVC.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>SpringMVC</servlet-name>
<url-pattern>*.action</url-pattern>
</servlet-mapping>
<filter>
<filter-name>CharacterEncodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CharacterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
</web-app>
3 applicationContext.xml
4 SpringMVC.xml
<beans>
<!-- 很重要 -->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="defaultEncoding" value="utf-8"></property>
</bean>
</beans>
5 Controller:
package com.bwie.controller;
import java.io.File;
import java.io.IOException;
import java.util.UUID;
import javax.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
@Controller
public class PowerController {
@RequestMapping("upload")
public void upload(HttpServletRequest request){
//擷取支援檔案上傳的Request對象 MultipartHttpServletRequest
MultipartHttpServletRequest mtpreq = (MultipartHttpServletRequest) request;
//通過 mtpreq 擷取檔案域中的檔案
MultipartFile file = mtpreq.getFile("file");
//通過MultipartFile 對象擷取檔案的原檔案名稱
String fileName = file.getOriginalFilename();
//產生一個uuid 的檔案名稱
UUID randomUUID = UUID.randomUUID();
//擷取檔案的尾碼名
int i = fileName.lastIndexOf(".");
String uuidName = randomUUID.toString()+fileName.substring(i);
//擷取伺服器的路徑地址(被上傳檔案的儲存地址)
String realPath = request.getSession().getServletContext().getRealPath("/file");
//將路徑轉化為檔案夾 並 判斷檔案夾是否存在
File dir = new File(realPath);
if(!dir.exists()){
dir.mkdir();
}
//擷取一個檔案的儲存路徑
String path = realPath+"/"+uuidName;
// 為檔案這伺服器中開闢一給新的空間,*沒有資料
// File newFile = new File(path);
try {
file.transferTo(new File(path));
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.err.println("-----伺服器的路徑地址為::"+realPath);
System.err.println("-----圖片名稱為::"+fileName);
System.err.println("-----圖片新名稱為::"+uuidName);
System.err.println("-----圖片新路徑為::"+path);
}
}