码迷,mamicode.com
首页 > Web开发 > 详细

SSM 实现文件上传

时间:2018-01-17 18:09:02      阅读:307      评论:0      收藏:0      [点我收藏+]

标签:pos   src   sys   上传   str   tag   app   www.   sub   

jar包

技术分享图片

配置文件

web.xml

<servlet>
<servlet-name>MyDispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 设置自己定义的控制器xml文件 -->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:mvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<!-- Spring MVC配置文件结束 -->

<!-- 拦截设置 -->
<servlet-mapping>
<servlet-name>MyDispatcher</servlet-name>
<!-- 由SpringMVC拦截所有请求 -->
<url-pattern>/</url-pattern>
</servlet-mapping>

spring mvc.xml

<!-- 上传文件的设置 ,maxUploadSize=-1,表示无穷大。uploadTempDir为上传的临时目录 -->
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="defaultEncoding" value="UTF-8"></property>
<property name="maxUploadSize" value="5400000"></property>
<property name="uploadTempDir" value="fileUpload/temp"></property>
</bean>

 

前台页面fileUpload.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>上传图片测试</title>
<base href="<%=basePath%>">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
<center>
<form action="file/upfile"
method="post" enctype="multipart/form-data">
<input type="file" name="file" />
<input type="submit" value="上 传" />
</form>
<h5>效果:</h5>
<img alt="图片" src="${file}" />
</center>
</body>
</html>

controller 层

 

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.UUID;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

 


@Controller
@RequestMapping("/file")
public class FileController {

@RequestMapping("/toFile")
public String toFileUpload() {
return "fileUpload";
}

@RequestMapping("/toFile2")
public String toFileUpload2() {
return "fileUpload2";
}

/**
* 上传文件
*/
@RequestMapping("/upfile")
public String oneFileUpload(
@RequestParam("file") CommonsMultipartFile file,
HttpServletRequest request, ModelMap model) {

// 获得原始文件名
String fileName = file.getOriginalFilename();
System.out.println("原始文件名:" + fileName);

// 新文件名
String newFileName = UUID.randomUUID() + fileName;

// 获得项目的路径
ServletContext sc = request.getSession().getServletContext();
// 上传位置
String path = sc.getRealPath("/img") + "/"; // 设定文件保存的目录

File f = new File(path);
if (!f.exists())
f.mkdirs();
if (!file.isEmpty()) {
try {
FileOutputStream fos = new FileOutputStream(path + newFileName);
InputStream in = file.getInputStream();
int b = 0;
while ((b = in.read()) != -1) {
fos.write(b);
}
fos.close();
in.close();
} catch (Exception e) {
e.printStackTrace();
}
}

System.out.println("上传图片到:/img/" + newFileName);
// 保存文件地址,用于JSP页面回显
String url = "/file/"+newFileName;
model.addAttribute("file", url);
return "fileUpload";
}
}

 

注意 配置Tomact 中service.xml  在<host></host>中加入

<Context path="/file"      docBase="D:\Program Files\Tomcat7.0\webapps\SSM\img" debug="0" reloadable="true"/>

 

SSM 实现文件上传

标签:pos   src   sys   上传   str   tag   app   www.   sub   

原文地址:https://www.cnblogs.com/ll0405/p/8303758.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!