使用SpringMVC上传文件

前端之家收集整理的这篇文章主要介绍了使用SpringMVC上传文件前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

下面是编程之家 jb51.cc 通过网络收集整理的代码片段。

编程之家小编现在分享给大家,也给大家做个参考。

package com.foo.controller;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

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
@RequestMapping(value="/file")
public class FileUpload{
	
	@RequestMapping(value="/gotofilepage")
	public String gotofilepage(){
		System.out.println("进入到file中的gotofilepage方法中。。。。");
		
		return "uploadfile";
	}
	
	@RequestMapping(value="/fileupload")
	public void fileUpload(HttpServletRequest request,HttpServletResponse response) {

		MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
		SimpleDateFormat dateformat = new SimpleDateFormat("yyyy/MM/dd/HH");
		/** 得到图片保存目录的真实路径 **/
		String realpath = request.getSession().getServletContext().getRealPath("/");
		/** 构建图片保存的目录 **/
		String filedir ="files" + File.separator
				+ dateformat.format(new Date());
		String filelocationdir = realpath + filedir;
		/** 根据真实路径创建目录 **/
		File logoSaveFile = new File(filelocationdir);
		if (!logoSaveFile.exists())
			logoSaveFile.mkdirs();
		/** 页面控件的文件流 **/
		MultipartFile multipartFile = multipartRequest.getFile("file");
		/** 获取文件的后缀,防止别人传可执行文件 **/
		String suffix = multipartFile.getOriginalFilename().substring(
				multipartFile.getOriginalFilename().lastIndexOf("."));

		String imagename = multipartFile.getOriginalFilename();
		/** 拼成完整的文件保存路径加文件 **/
		String filename = filelocationdir + File.separator + imagename;
		File file = new File(filename);

		try {
			multipartFile.transferTo(file);
		} catch (IllegalStateException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

}
<body>
<h1>文件上传</h1>
<form action="./fileupload.action" method="post" enctype="multipart/form-data">			
<input type="file" name="file" />
<input type="submit" value="upload" />
</form>
</body>

springmvc-servlet.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:aop="http://www.springframework.org/schema/aop"
	xmlns:task="http://www.springframework.org/schema/task" 
	xsi:schemaLocation="
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
        http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd">

	<!-- @Controller,@Service,@Configuration,etc. -->
	<context:component-scan base-package="com.foo.controller" />	
	
	<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="prefix" value="/WEB-INF/views/" />
		<property name="suffix" value=".jsp" />
	</bean>
	<!-- 上传文件必须加 -->
	<bean id="multipartResolver"
		class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
		<!-- set the max upload size100MB -->
		<property name="maxUploadSize">
			<value>104857600</value>
		</property>
		<property name="maxInMemorySize">
			<value>4096</value>
		</property>
	</bean>
<!-- 	<task:annotation-driven/>
	<bean id="taskTest" class="com.htf.task.MyTask"></bean>
	<task:scheduled-tasks>
		  <task:scheduled ref="taskTest" method="say" cron="5/3 * * * * ?" />  
	</task:scheduled-tasks>
	<context:component-scan base-package="com.htf.task" /> -->
</beans>

commons-fileupload-1.3.1.jar

以上是编程之家(jb51.cc)为你收集整理的全部代码内容,希望文章能够帮你解决所遇到的程序开发问题。

如果觉得编程之家网站内容还不错,欢迎将编程之家网站推荐给程序员好友。

原文链接:https://www.f2er.com/springmvc/461591.html

猜你在找的SpringMVC相关文章