2
13693261870
2022-09-16 653761a31dfeb50dd3d007e892d69c90bf0cdafc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
package com.landtool.lanbase.modules.res.controller;
 
 
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.UUID;
 
import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
 
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.landtool.lanbase.config.SysTemPropertyConfig;
import com.landtool.lanbase.modules.api.utils.DeleteFileUtils;
import com.landtool.lanbase.modules.api.utils.PageBean;
import com.landtool.lanbase.modules.res.entity.Res_Files;
import com.landtool.lanbase.modules.res.service.ResFilesService;
import com.landtool.lanbase.modules.res.service.ResMainInfoService;
 
/**
 * @Author: xiaoqi
 * @Date: 2018-04-19  14:49
 * @Description:11系统资源扩展(附件管理)(Res_Files)
 *
 */
 
@Controller
@RequestMapping("/res")
public class ResFilesController {
    @Autowired
    private ResFilesService resFilesService;
 
//    @Autowired
//    public org.springframework.core.env.Environment env;
 
    @Autowired
    private SysTemPropertyConfig  sysConfig;
    
    @Autowired
    private ResMainInfoService resMainInfoService;
 
    private Integer MainInfoId=null;  //当前选中的资源ID
    /**
     * 访问附件管理数据图层信息页面
     */
    @RequestMapping("/ResManage/ResRegister/ResFiles")
    public String ResFiles(int resMainInfoId,Model datamodel) {
        MainInfoId=resMainInfoId;  //获取当前资源ID
        List<Res_Files> List = resFilesService.selectDataListForResourceid(resMainInfoId);
        String result = "[";
        for (Res_Files model : List) {
            result += "{ filename:" + model.getFilename() + "},";
        }
        if (List.size() > 0) {
            result = result.substring(0, result.length() - 1);
        }
        result += "]";
        return "ResManage/ResRegister/ResFiles";
    }
 
    /**
     * 保存文件数据
     */
    @ResponseBody
    @RequestMapping("/resFiles/insertFileData")
    public int insertFileData(Res_Files record, double savedsizenum,String filetype) {
        record.setFilesize(savedsizenum);
        record.setFiletype(filetype);
        TiHuanLuJin(record.getServerurl());
        record.setServerurl(record.getServerurl().replace("temp/",""));
        return resFilesService.insertSelective(record);
    }
 
    //替换路径
    private void TiHuanLuJin(@RequestBody String lujin) {
        String oldFileUrl=sysConfig.getUploadPath()+lujin.replace("/","\\");
        File oldFile=new File(oldFileUrl);
        String NewFileUrl=sysConfig.getUploadPath()+lujin.replace("temp","").replace("/","\\");
        NewFileUrl=NewFileUrl.substring(0,NewFileUrl.lastIndexOf("\\"))+"\\" ;
        File NewFile=new File(NewFileUrl);
        if (!NewFile.exists()) { // 当前地址不为空,判断该路径是否存在,不存在则创建新的文件夹
            File newfilePath = new File(NewFile+"\\"); // 创建对应的年月文件夹
            newfilePath.mkdirs();
        }
        com.landtool.lanbase.common.utils.FileUtils.moveTotherFolders(oldFileUrl,NewFileUrl);
    }
 
    /**
     * 删除文件数据
     */
    @ResponseBody
    @RequestMapping("/resFiles/deleteFileData")
    public int deleteFileData(int fileid) {
        Res_Files resFiles=resFilesService.selectByPrimaryKey(fileid);
        if(resFiles==null){
            return 0;
        }else {
            String path = sysConfig.getUploadPath() + resFiles.getServerurl();
            DeleteFileUtils.deletefile(path);
            return resFilesService.deleteByPrimaryKey(fileid);
        }
 
    }
 
    /**
     * 文件上传
     */
    @ResponseBody
    @RequestMapping("/resFile/uploadfile")
    public String uploadfile(@RequestParam("myFile") MultipartFile file, String resourceid) {
        File desFile = null;
        if (file.isEmpty()) {
            return "No File";
        }
        String uuid = UUID.randomUUID().toString().replaceAll("-", "");
        String name = file.getOriginalFilename();
        String fileType = name.substring(name.lastIndexOf("."));
        String Filename = uuid + fileType;
        String size = FileUtils.byteCountToDisplaySize(file.getSize());
 
        String webpath="";
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
        String timePath = sdf.format(new Date().getTime());
        timePath = timePath.replace("-", "");
        Filename = uuid + fileType;
        String path = null;
 
        path = sysConfig.getUploadPath() + "temp\\FileSource\\wenjian\\"+timePath + "\\resfile\\";
        File filePath = new File(path); // 创建对应的年月文件夹
        if (!filePath.exists()) {
            filePath.mkdirs();
        }
        //文件网络地址
        webpath ="temp/FileSource/wenjian/"+ timePath + "/resfile/" + Filename; // 类型为文件指向文件
 
        desFile = new File(path + Filename);
        String info = desFile.getAbsolutePath();
        System.out.println("info:" + info);
        try {
            FileUtils.copyInputStreamToFile(file.getInputStream(), desFile);
            return "{'result':'1','Path':'"+webpath+"','Filename':'"+name.substring(name.lastIndexOf("\\")+1,name.lastIndexOf("."))+"','size':'"+size.substring(0,size.lastIndexOf(" "))+"','unit':'"+size.substring(size.lastIndexOf(" ")+1,size.length())+"','FileType':'"+fileType+"'}";
        } catch (IOException e) {
            e.printStackTrace();
            return "{'result':'0'}";
        }
    }
 
    /**
     *加载Ext数据
     */
    @ResponseBody
    @RequestMapping("/resFiles/Getfiledata")
    public String Getfiledata(Integer resMainInfoId, Model datamodel, PageBean pageBean) {
        Page<Res_Files> page = PageHelper.startPage(pageBean.getPage(), pageBean.getLimit());
        //alter Xxx 20181212
        List<Res_Files> List = resFilesService.selectDataListForResourceid(resMainInfoId);
        int countNums = (int) ((Page) List).getTotal();
        PageBean<Res_Files> pageData = new PageBean<>(pageBean.getPage(), pageBean.getLimit(), countNums);
        pageData.setItems(List);
 
        String result = "{";
        result += "\"Count\":" + countNums + ",\"list\":[";
        for (Res_Files model : List) {
            String SizeUnit="";
            double Filesize=model.getFilesize();
            if(Filesize>0&&Filesize<1000){
                if(Filesize<1){SizeUnit=Filesize+"KB";}
                else{  SizeUnit=Filesize+"";
                SizeUnit=SizeUnit.substring(0,SizeUnit.length()-2)+"KB"; }
            }
            else if((Filesize/1000/1000)<1){
                SizeUnit=(Filesize/1000)+"";
                SizeUnit=SizeUnit.substring(0,SizeUnit.length()-2)+"MB";
            }
            else if((Filesize/1000/1000)>1){
                SizeUnit=(Filesize/1000/1000)+"";
                SizeUnit=SizeUnit.substring(0,SizeUnit.length()-2)+"GB";
            }
            else {SizeUnit="0";}
            result += "{ \"filename\":"+"\""+ model.getFilename() +"\""+ ","+"\"fileid\":"+"\""+ model.getFileid() +"\""+","+"\"filetype\":"+"\""+ model.getFiletype() +"\""+","+"\"filesize\":"+"\""+ SizeUnit +"\""+","+"\"serverurl\":"+"\""+ model.getServerurl() +"\""+"},";
        }
        if (List.size() > 0) {
            result = result.substring(0, result.length() - 1);
        }
        result += "]}";
        datamodel.addAttribute("pubzyWebRoot", sysConfig.getPubzyWebRoot());
        return result;
    }
}