您的位置:首页 > 编程语言 > Java开发

spring上传文件

2015-09-24 14:58 465 查看
package com.springapp.mvc;

import org.apache.commons.io.FileUtils;

import org.aspectj.lang.annotation.After;

import org.aspectj.lang.annotation.Aspect;

import org.aspectj.lang.annotation.Before;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Controller;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestMethod;

import org.springframework.web.bind.annotation.RequestParam;

import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;

import java.io.File;

import java.io.IOException;

import com.yangzhuo.Point.Log;

/**

* Created by yangzhuo02 on 2015/9/7.

*/

@Controller

@RequestMapping("/")

public class FileUploadController {

@Autowired

private com.springapp.mvc.Service service;

@RequestMapping(value = "/upload", method = RequestMethod.POST)

public String fileupload( @RequestParam("name") String name, @RequestParam("uploadfile") MultipartFile uploadfile,

HttpServletRequest httpServletRequest) throws IOException{

//使用此方法获取文件存放路径

String realPath = httpServletRequest.getSession().getServletContext().getRealPath("/WEB-INF/upload");

service.sayHello();

if (!uploadfile.isEmpty()) {

//必须使用getOriginalFilename否则会因为无法获取文件名字而报错

FileUtils.copyInputStreamToFile(uploadfile.getInputStream(), new File(realPath, uploadfile.getOriginalFilename()));

return "success";

} else {

return "error";

}

}

}

jsp文件:

<%--

Created by IntelliJ IDEA.

User: yangzhuo02

Date: 2015/9/7

Time: 11:03

To change this template use File | Settings | File Templates.

--%>

<%@ page contentType="text/html;charset=UTF-8" language="java" %>

<html>

<body>

<form method="post"

action="/upload" enctype="multipart/form-data">

File to upload: <input type="file" name="uploadfile"><br/>

name: <input type="text" name="name">

<input type="submit" value="Upload"> Press here to upload the file!

</form>

<%--<form action="/upload" method="post">--%>

<%--name:<input type="text" name="name">--%>

<%--<input type="submit"/>--%>

<%--</form>--%>

</body>

</html>

配置文件中比较重要的部分

<!-- SpringMVC上传文件时,需要配置MultipartResolver处理器 -->

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

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

<!-- 指定所上传文件的总大小不能超过200KB。注意maxUploadSize属性的限制不是针对单个文件,而是所有文件的容量之和 -->

<property name="maxUploadSize" value="2000000"/>

</bean>

<!-- SpringMVC在超出上传文件限制时,会抛出org.springframework.web.multipart.MaxUploadSizeExceededException -->

<!-- 该异常是SpringMVC在检查上传的文件信息时抛出来的,而且此时还没有进入到Controller方法中 -->

<bean id="exceptionResolver" class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">

<property name="exceptionMappings">

<props>

<!-- 遇到MaxUploadSizeExceededException异常时,自动跳转到/WEB-INF/jsp/error_fileupload.jsp页面 -->

<prop key="org.springframework.web.multipart.MaxUploadSizeExceededException">error</prop>

</props>

</property>

</bean>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: