java对文件夹获取、删除、保存文件

java对文件夹获取、删除、保存文件

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.PathResource;
import org.springframework.jdbc.datasource.init.ScriptUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import javax.sql.DataSource;
import java.io.File;
import java.sql.Connection;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping(value = "/SystemManage/SM_DatabaseManage")
public class SM_Database_Controller {

    @Autowired
    private DataSource dataSource;

    @RequestMapping("/MyList")
    public String MyList(HttpServletRequest request, Model model){
        return "SystemManage/SM_DatabaseManage/SM_Database_MyList";
    }

    @RequestMapping("/Add")
    public String AddDialog(){
        return "SystemManage/SM_DatabaseManage/SM_Database_Edit";
    }

    //从文件夹中获取文件
    @RequestMapping("/GetList")
    @ResponseBody
    public Object GetList(HttpServletRequest request) throws Exception{
        List<AM_Sql> am_sqls = new ArrayList<>();
        String realPath = request.getSession().getServletContext().getRealPath("/template");
        realPath = realPath.replaceAll("\\\\","/");
        File dir = new File(realPath);
        List<File> allFileList = new ArrayList<>();
        getAllFile(dir, allFileList);
        for (File file : allFileList) {
            AM_Sql am_sql = new AM_Sql();
            am_sql.setFilename(file.getName());
            am_sqls.add(am_sql);
        }
        HashMap<String, Object> map = new HashMap<>();
        map.put("rows",am_sqls);
        return map;
    }

    //向文件夹中添加文件
    @RequestMapping("/insertSqlFile")
    @ResponseBody
    public Integer insertSqlFile(HttpServletRequest request){
        try {
            if (request instanceof MultipartHttpServletRequest) {
                MultipartHttpServletRequest req = (MultipartHttpServletRequest) request;
                Map<String, MultipartFile> fileMap = req.getFileMap();
                MultipartFile file = fileMap.get("file");
                String realPath = request.getSession().getServletContext().getRealPath("/template");
                File file1 = new File(realPath);
                if (!file1.exists()) {
                    file1.mkdirs();
                }
                realPath = realPath.replaceAll("\\\\","/");
                FileUtil.saveMultipartFile(file, realPath);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        }
        return 1;
    }

    //删除文件夹中的某个文件
    @RequestMapping("/delInfoById")
    @ResponseBody
    public Integer delInfoById(HttpServletRequest request, String filename) {
        try {
            String realPath = request.getSession().getServletContext().getRealPath("/template/");
            realPath = realPath.replaceAll("\\\\","/");
            String filepath = realPath + filename;
            FileUtil.delFile(filepath);
            return 1;
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        }
    }

    public static void getAllFile(File fileInput, List<File> allFileList) {
        // 获取文件列表
        File[] fileList = fileInput.listFiles();
        for (File file : fileList) {
            if (file.isDirectory()) {
                // 递归处理文件夹
                // 如果不想统计子文件夹则可以将下一行注释掉
                getAllFile(file, allFileList);
            } else {
                // 如果是文件则将其加入到文件数组中
                allFileList.add(file);
            }
        }
    }
public static String saveMultipartFile(MultipartFile multipartFilepath, String destinationAddress) throws IllegalStateException, IOException{
		String fileName = "";
		if(multipartFilepath!=null){
			fileName =  multipartFilepath.getOriginalFilename();
			//为防止文件名重复,给文件名前面添加日期前缀
		    fileName = newDate.getDate("yyyyMMdd")+(new Random().nextInt(900000)+100000)+fileName;
		    File file=new File(destinationAddress);
		    if(!file.exists()){
		    	file.mkdirs();
		    }
		    file=new File(destinationAddress,fileName);
		    multipartFilepath.transferTo(file);
		}
		return fileName;
	}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值