博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Java Web(SpringBoot 2.x)上传文件夹,完整代码
阅读量:2388 次
发布时间:2019-05-10

本文共 3334 字,大约阅读时间需要 11 分钟。

1、pom.xml

org.springframework.boot
spring-boot-starter-web
commons-io
commons-io
2.4
commons-fileupload
commons-fileupload
1.3.3

2、SpringBootApplication

package cn.hadron;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.boot.web.servlet.MultipartConfigFactory;import org.springframework.context.annotation.Bean;import javax.servlet.MultipartConfigElement;@SpringBootApplicationpublic class DbApplication {    public static void main(String[] args) {        SpringApplication.run(DbApplication.class, args);    }    @Bean    public MultipartConfigElement multipartConfigElement() {        MultipartConfigFactory factory = new MultipartConfigFactory();        //单个文件最大   KB,MB        factory.setMaxFileSize("100MB");        /// 设置总上传数据总大小        factory.setMaxRequestSize("500MB");        return factory.createMultipartConfig();    }}

3、controller

package cn.hadron.controller;importcn.hadron.db.util.FileUtil;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.ModelAttribute;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 java.io.File;import java.io.IOException;@Controller@RequestMapping(value = "/file")public class FileController {    @RequestMapping(value="/upload.do",method= RequestMethod.POST)    public String upload(@RequestParam("dir") MultipartFile[] dir) {        System.out.println("上传文件夹...");        File file;        String fileName="";        String filePath="";        for (MultipartFile f : dir) {            fileName=f.getOriginalFilename();            String type=f.getContentType();            System.out.println("\n"+fileName+" ,"+type);            filePath="D:\\upload\\"+fileName.substring(0,fileName.lastIndexOf("/"));            if(!FileUtil.isDir(filePath)){                FileUtil.makeDirs(filePath);            }            file = new File("D:\\upload\\" + fileName);            try {                file.createNewFile();                //将上传文件保存到一个目标文件中                f.transferTo(file);            } catch (IOException e) {                e.printStackTrace();            }        }        return "filelist";    }}
package cn.hadron.db.util;import java.io.*;import java.util.ArrayList;import java.util.List;import org.apache.commons.io.FileUtils;/** * 文件操作工具类 * * @author cyq * */public class FileUtil {	public static boolean isFile(String filepath) {		File f = new File(filepath);		return f.exists() && f.isFile();	}	public static boolean isDir(String dirPath) {		File f = new File(dirPath);		return f.exists() && f.isDirectory();	}	/**	 * 创建多级目录	 * @param path	 */	public static void makeDirs(String path) {		File file = new File(path);		// 如果文件夹不存在则创建		if (!file.exists() && !file.isDirectory()) {			file.mkdirs();		}else {			System.out.println("创建目录失败:"+path);		}	}}

4、上传页面

    
Title

在这里插入图片描述

转载地址:http://grvab.baihongyu.com/

你可能感兴趣的文章
淺談以STIX實現網路威脅情報標準化框架
查看>>
Top IT management trends - the next 5 years
查看>>
推荐 OWASP - Transport Layer Protection Cheat Sheet
查看>>
AutoNessus v1.3.2 released
查看>>
hack tools
查看>>
rhel5中管理swap空间
查看>>
/proc filesystem allows bypassing directory permissions on Linux
查看>>
nginx dos
查看>>
RASP解决方案包括开源方案
查看>>
Linux下共享文件系统文件传输的简单设计(转载)
查看>>
点评Ubuntu下的文件安全删除工具
查看>>
数据可视化
查看>>
Security Ressources Sites
查看>>
mysql的比较运算
查看>>
Data Breach Report
查看>>
再探偏移注射
查看>>
DNS Security Tips
查看>>
符号执行
查看>>
Remote Installation Service (RIS) in Windows Server 2003
查看>>
Layer Four Traceroute
查看>>