From fa00382db413a8489d07983b66f0869305b51a9a Mon Sep 17 00:00:00 2001 From: 81041 <81041@DESKTOP-025NVD9> Date: 星期五, 11 一月 2019 11:35:54 +0800 Subject: [PATCH] 批量读取系统参数 --- gx_tieta/src/com/fgkj/actions/MyFileAction.java | 123 +++++++++++++++++++++++++++++++++++++++++ 1 files changed, 123 insertions(+), 0 deletions(-) diff --git a/gx_tieta/src/com/fgkj/actions/MyFileAction.java b/gx_tieta/src/com/fgkj/actions/MyFileAction.java index faf8a3d..8e6bd5f 100644 --- a/gx_tieta/src/com/fgkj/actions/MyFileAction.java +++ b/gx_tieta/src/com/fgkj/actions/MyFileAction.java @@ -2,16 +2,23 @@ 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; // 鎻愪氦杩囨潵鐨刦ile鐨勫悕瀛� @@ -167,6 +174,107 @@ } return flag; } + + //涓婁紶鏈烘埧瑙嗛鎺ュ彛 + public String uploadStationFile(){ + BattInf binf = getGson().fromJson(json, BattInf.class); + String fileRoot = "stationsrc"; + ServiceModel model = new ServiceModel(); + boolean isSuccess = false; + String loadpath = ActionUtil.getSession().getServletContext().getRealPath("/"); + String str = new File(loadpath).getParentFile().getAbsolutePath(); //鑾峰彇鏈嶅姟鍣ㄦ墍鍦ㄧ殑缁濆璺緞 + String root = str+"/"+fileRoot+"/"+binf.getStationId()+"/"+binf.getVideoOrImage()+"/"; // 涓婁紶璺緞 + for(int i=0;i<file.length && binf != null;i++){ + String filePath = root+fileFileName[i]; + createFilefolderIFNotExist(filePath); + copyFile(file[i], filePath); + isSuccess = true; + } + if(isSuccess){ + model.setCode(1); + model.setMsg("涓婁紶鎴愬姛"); + }else{ + model.setCode(0); + model.setMsg("涓婁紶澶辫触"); + } + result = tojson(model); + return SUCCESS; + } + + /** + * + * 鑾峰彇鎸囧畾鏈烘埧寰楄祫婧� + */ + public String searchStationSource(){ + String fileRoot = "stationsrc"; //瀛樻斁鏈烘埧璧勬簮鏂囦欢鐨勬枃浠跺す + BattInf binf = getGson().fromJson(json, BattInf.class); + ServiceModel model = new ServiceModel(); + String loadpath = ActionUtil.getSession().getServletContext().getRealPath("/"); + String str = new File(loadpath).getParentFile().getAbsolutePath(); + if(binf != null && binf.getStationId() != null && binf.getVideoOrImage() != null){ + String filesource = str+File.separator+fileRoot+File.separator+binf.getStationId()+File.separator+binf.getVideoOrImage()+File.separator; //褰撳墠璇锋眰鐨勬満鎴挎墍鍦ㄧ殑璧勬簮鏂囦欢澶� + //System.out.println(filesource); + File source = new File(filesource); + File[] files = source.listFiles(); + model.setMsg("鏆傛棤瑙嗛鏂囦欢"); + if(files!=null && files.length>0){ + List myfiles = new ArrayList<MyFile>(); + for(int i=0;i<files.length;i++){ + MyFile myfile = new MyFile(); + if(files[i].isFile()){ + myfile.setFile(files[i]); + myfiles.add(myfile); + } + } + if(myfiles.size() > 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; @@ -196,4 +304,19 @@ 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)); + + } } -- Gitblit v1.9.1