`
wiselyman
  • 浏览: 2081119 次
  • 性别: Icon_minigender_1
  • 来自: 合肥
博客专栏
Group-logo
点睛Spring4.1
浏览量:81103
74ae1471-94c5-3ae2-b227-779326b57435
点睛Spring MVC4...
浏览量:130136
社区版块
存档分类
最新评论

06点睛Spring MVC 4.1-文件上传

 
阅读更多

6.1 文件上传

  • 在控制器参数使用@RequestParam("file") MultipartFile file接受单个文件上传;
  • 在控制器参数使用@RequestParam("file") MultipartFile[] files接受多个文件上传;
  • 通过配置MultipartResolver来配置文件上传的一些属性;

6.2 示例

  • 增加和上传和文件操作的依赖到maven
<dependency>
    <groupId>commons-io</groupId>
    <artifactId>commons-io</artifactId>
    <version>2.3</version>
</dependency>
<dependency>
    <groupId>commons-fileupload</groupId>
    <artifactId>commons-fileupload</artifactId>
    <version>1.3.1</version>
</dependency>
  • 上传控制器
package com.wisely.web;

import java.io.File;
import java.io.IOException;

import org.apache.commons.io.FileUtils;
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.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

@Controller
public class UploadController {

    //接受多个文件上传使用@RequestParam("file") MultipartFile[] files
    @RequestMapping(value = "/upload",method = RequestMethod.POST)
    public @ResponseBody String upload(@RequestParam("file") MultipartFile file) {

            try {
    FileUtils.writeByteArrayToFile(new File("e:/"+file.getOriginalFilename()),
                                                               file.getBytes());
                return "ok";
            } catch (IOException e) {
                e.printStackTrace();
                return "wrong";
            }


    }

}
  • 文件上传所需配置
@Configuration
@ComponentScan("com.wisely")
@EnableWebMvc
public class DemoMVCConfig extends WebMvcConfigurerAdapter {

    @Bean
    public UrlBasedViewResolver viewResolver(){
        UrlBasedViewResolver resolver = new UrlBasedViewResolver();
        resolver.setPrefix("/WEB-INF/views/")
        resolver.setSuffix(".jsp");
        resolver.setViewClass(JstlView.class);
        return resolver;
    }
    //注册拦截器
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
    registry.addInterceptor(demoInteceptor());
    }
    //自定义拦截器
    @Bean
    public DemoInteceptor demoInteceptor(){
        return new DemoInteceptor();
    }

    //静态资源映射
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/js/**").addResourceLocations("/js/");
    }
    //文件上传设置--在此处
    @Bean
    public MultipartResolver multipartResolver() {
        CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver();
        multipartResolver.setMaxUploadSize(1000000);
        return multipartResolver;
    }
}
  • 页面代码
<form action="upload" enctype="multipart/form-data" method="post">
    <input type="file" name="file"/><br/>
    <input type="submit" value="上传">
</form>

新书推荐《JavaEE开发的颠覆者: Spring Boot实战》,涵盖Spring 4.x、Spring MVC 4.x、Spring Boot企业开发实战。

 

京东地址:http://item.jd.com/11894632.html

当当地址:http://product.dangdang.com/23926195.html

亚马逊地址:http://www.amazon.cn/图书/dp/B01D5ZBFUK/ref=zg_bsnr_663834051_6 

淘宝地址:https://item.taobao.com/item.htm?id=528426235744&ns=1&abbucket=8#detail

 

 

 

或自己在京东、淘宝、亚马逊、当当、互动出版社搜索自选。

 


0
0
分享到:
评论
2 楼 wiselyman 2015-05-27  
dlj 写道
报错了呢:Could not instantiate bean class [org.springframework.web.multipart.MultipartFile]: Specified class is an interface


@RequestParam("file") MultipartFile file
@RequestParam("file")

你可加了?
1 楼 dlj 2015-05-27  
报错了呢:Could not instantiate bean class [org.springframework.web.multipart.MultipartFile]: Specified class is an interface

相关推荐

Global site tag (gtag.js) - Google Analytics