package com.fgkj.actions; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.util.ArrayList; import java.util.List; import javax.servlet.http.HttpServletResponse; import com.fgkj.dto.BattInf; import com.fgkj.dto.MyFile; import com.fgkj.dto.ServiceModel; import com.google.gson.Gson; public class MyFileAction extends ActionUtil{ private String result; private String json; private String battname; private File[] file; // 提交过来的file的名字 private String[] fileFileName; // 提交过来的file的MIME类型 private String[] fileContentType; private String filestr; //文件内容 private String fname; //文件内容 //上传文件 public String UploadTestFile(){ HttpServletResponse res = ActionUtil.getResponse(); res.setHeader("Access-Control-Allow-Origin", "*"); //允许跨域访问 res.setHeader("Access-Control-Allow-Headers", "X-Requested-With,content-type,token"); res.setHeader("Access-Control-Allow-Methods", "GET, HEAD, POST, PUT, DELETE, TRACE, OPTIONS, PATCH"); boolean f = createFile(); ServiceModel model = new ServiceModel(); if(f){ model.setCode(1); model.setMsg("上传成功"); }else{ model.setCode(0); model.setMsg("上传失败"); } result = ActionUtil.tojson(model); return SUCCESS; } public boolean createFile(){ String loadpath = ActionUtil.getSession().getServletContext().getRealPath("/"); String str = new File(loadpath).getParentFile().getAbsolutePath(); boolean flag = false; String root = str+"/uploadfiles/"; // 上传路径 //System.out.println(battname+"***"); for(int i=0;i0){ List myfiles = new ArrayList(); for(int i=0;i 0){ model.setCode(1); model.setData(myfiles); model.setMsg("读取成功"); } } } result = tojson(model); return SUCCESS; } //将文件复制到指定的目录 public static void copyFile(File oldFile, String newPath){ File file = new File(newPath); FileInputStream in = null; FileOutputStream out = null; try { in = new FileInputStream(oldFile); out = new FileOutputStream(file);; byte[] buffer=new byte[2097152]; int readByte = 0; while((readByte = in.read(buffer)) != -1){ out.write(buffer, 0, readByte); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { if(in != null){ try { in.close(); } catch (IOException e) { e.printStackTrace(); } } if(out != null){ try { out.close(); } catch (IOException e) { e.printStackTrace(); } } } } public void setFilestr(String filestr) { this.filestr = filestr; } public void setFname(String fname) { this.fname = fname; } public void setFile(File[] file) { this.file = file; } public void setFileFileName(String[] fileFileName) { this.fileFileName = fileFileName; } public void setFileContentType(String[] fileContentType) { this.fileContentType = fileContentType; } public String getResult() { return result; } public void setBattname(String battname) { this.battname = battname; } public void setJson(String json) { this.json = json; } public static void main(String[] args) { String filePath = "D:/test/a/a.txt"; //ActionUtil.createFileRootIFNotExist(filePath); File file = new File(filePath); System.out.println(ActionUtil.tojson(file)); } }