LiJun
2018-11-17 7ce7005333154eb9a0da1461604bcc0e06f1b4cc
视频文件上传之  读取当前机房的视频文件
1个文件已添加
1个文件已修改
107 ■■■■■ 已修改文件
gx_tieta/src/com/fgkj/actions/MyFileAction.java 51 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
gx_tieta/src/com/fgkj/dto/MyFile.java 56 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
gx_tieta/src/com/fgkj/actions/MyFileAction.java
@@ -5,10 +5,13 @@
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;
@@ -179,9 +182,9 @@
        ServiceModel model = new ServiceModel();    
        boolean isSuccess = false;
        String loadpath = ActionUtil.getSession().getServletContext().getRealPath("/");
        String str = new File(loadpath).getParentFile().getAbsolutePath();                    //获取服务器所在的绝对路径
        String str = new File(loadpath).getParentFile().getAbsolutePath();                                                    //获取服务器所在的绝对路径
        String root = str+"/"+fileRoot+"/"+binf.getStationId()+"/video/";                                                    // 上传路径
        for(int i=0;i<file.length;i++){
        for(int i=0;i<file.length && binf != null;i++){
            String filePath = root+fileFileName[i];
            createFilefolderIFNotExist(filePath);
            copyFile(file[i], filePath);
@@ -198,7 +201,44 @@
        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.getStationName() != null){
            String filesource = str+File.separator+fileRoot+File.separator+binf.getStationId()+File.separator+binf.getStationName()+File.separator;                //当前请求的机房所在的资源文件夹
            System.out.println(filesource);
            File source = new File(filesource);
            File[] files = source.listFiles();
            model.setMsg("暂无视频文件");
            if(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;
@@ -233,6 +273,7 @@
            }
        }
    }
    public void setFilestr(String filestr) {
@@ -272,6 +313,10 @@
    public static void main(String[] args) {
        String filePath = "D:/test/a/a.txt";
        ActionUtil.createFileRootIFNotExist(filePath);
        //ActionUtil.createFileRootIFNotExist(filePath);
        File file = new File(filePath);
        System.out.println(ActionUtil.tojson(file));
    }
}
gx_tieta/src/com/fgkj/dto/MyFile.java
New file
@@ -0,0 +1,56 @@
package com.fgkj.dto;
import java.io.File;
public class MyFile {
    private File file;
    private String fileName;        //文件名称
    private String filePath;        //文件路径
    private String fileType;        //文件类型
    public void initFile(){
        this.fileName = file.getName();
        this.filePath = file.getAbsolutePath();
        this.fileType = getFileTypeFromFile(this.file);
    }
    public String getFileTypeFromFile(File source){
        String[] str = source.getAbsolutePath().split("\\.");
        return str[str.length-1];
    }
    public File getFile() {
        return file;
    }
    public void setFile(File file) {
        this.file = file;
        initFile();
    }
    public String getFileName() {
        return fileName;
    }
    public void setFileName(String fileName) {
        this.fileName = fileName;
    }
    public String getFilePath() {
        return filePath;
    }
    public void setFilePath(String filePath) {
        this.filePath = filePath;
    }
    public String getFileType() {
        return fileType;
    }
    public void setFileType(String fileType) {
        this.fileType = fileType;
    }
    @Override
    public String toString() {
        return "MyFile [file=" + file + ", fileName=" + fileName
                + ", filePath=" + filePath + ", fileType=" + fileType + "]";
    }
}