zoukankan      html  css  js  c++  java
  • Spring Uploading Files

    1,在servlet-dispatcher.xml中添加代码

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


    也可以根据需求添加相关属性
    <property name="maxUploadSize" value="2097152"></property>   

    2,添加依赖jar文件

    		<dependency>
    			<groupId>commons-io</groupId>
    			<artifactId>commons-io</artifactId>
    			<version>2.4</version>
    		</dependency>
    		<dependency>
    			<groupId>commons-fileupload</groupId>
    			<artifactId>commons-fileupload</artifactId>
    			<version>1.3.1</version>
    		</dependency>

    3、编写uploadController

        @RequestMapping(value="/upload",method=RequestMethod.POST)  
        public String processUpload(@RequestParam("name") String name, 
        		@RequestParam("file") MultipartFile file) throws IOException {  
          	log.info("File '" + file.getOriginalFilename() + "' uploaded successfully");
          	if (!file.isEmpty()) {
                try {
                    byte[] bytes = file.getBytes();
                    BufferedOutputStream stream =
                            new BufferedOutputStream(new FileOutputStream(new File(name)));
                    stream.write(bytes);
                    stream.close();
                    
                    
                    
                    return "You successfully uploaded " + name + "!";
                } catch (Exception e) {
                    return "You failed to upload " + name + " => " + e.getMessage();
                }
            } else {
                return "You failed to upload " + name + " because the file was empty.";
            }
        }  

    4、使用RestClient测试上传


    后台提示:

    [2015-08-18 11/:36/:12]INFO  com.kaishuhezi.api.hardware.log.controller.LogController(line/:40) -File '78a0f7dcjw1e1bvyuzt1jj.jpg' uploaded successfully


  • 相关阅读:
    pgrep 查询进程的工具
    shell脚本中>/dev/null的含义
    一条命令批量替换多个文件中字符串
    Centos 6.4上面用Shell脚本一键安装vsftpd
    centos 卸载vsftpd方法
    Centos 6.4上面用Shell脚本一键安装mysql 5.6.15
    SHELL学习笔记----IF条件判断,判断条件
    CentOS挂载新硬盘
    Linux fdisk 命令
    Linux df 命令
  • 原文地址:https://www.cnblogs.com/duyinqiang/p/5696577.html
Copyright © 2011-2022 走看看