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;
}
}