图片上传/文件上传
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);
}
}