This commit is contained in:
parent
2adae0e122
commit
510e8486a8
|
|
@ -0,0 +1,14 @@
|
|||
import axios from "axios";
|
||||
import {getToken} from "@/utils/auth";
|
||||
|
||||
export function downloadFile(fileId){
|
||||
axios({
|
||||
method:'get',
|
||||
url:process.env.VUE_APP_BASE_API+'/system/file/download/'+fileId,
|
||||
headers:{
|
||||
token:'Bearer ' + getToken() // 让每个请求携带自定义token 请根据实际情况自行修改
|
||||
}
|
||||
}).then(res=>{
|
||||
return res;
|
||||
})
|
||||
}
|
||||
|
|
@ -62,5 +62,12 @@ export function getRoleAll(){
|
|||
type:2
|
||||
}
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
export function downloadFile(fileId){
|
||||
return request({
|
||||
url:'/system/file/download/'+fileId,
|
||||
method:'get'
|
||||
})
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -152,6 +152,13 @@
|
|||
v-hasPermi="['system:file:remove']"
|
||||
>删除
|
||||
</el-button>
|
||||
<el-button
|
||||
size="mini"
|
||||
type="text"
|
||||
icon="el-icon-delete"
|
||||
@click="handleDownload(scope.row)"
|
||||
>下载
|
||||
</el-button>
|
||||
</template>
|
||||
</el-table-column>
|
||||
</el-table>
|
||||
|
|
@ -210,6 +217,7 @@
|
|||
|
||||
<script>
|
||||
import {listFile, getFile, delFile, addFile, updateFile, exportFile, getRoleAll} from "@/api/system/file";
|
||||
import {downloadFile} from '@/api/system/download'
|
||||
import ImageUpload from '@/components/ImageUpload';
|
||||
import FileUpload from '@/components/FileUpload';
|
||||
|
||||
|
|
@ -279,7 +287,6 @@ export default {
|
|||
this.isPublicOptions = response.data;
|
||||
});
|
||||
getRoleAll().then(res => {
|
||||
console.log(res)
|
||||
this.roleOptions = res.data
|
||||
})
|
||||
},
|
||||
|
|
@ -328,6 +335,19 @@ export default {
|
|||
this.fileForm.open = false;
|
||||
this.reset();
|
||||
},
|
||||
// 下载按钮
|
||||
handleDownload(row){
|
||||
this.$confirm("是否确认下载"+row.fileName+"?","警告",{
|
||||
confirmButtonText: "确定",
|
||||
cancelButtonText: "取消",
|
||||
type: "warning"
|
||||
}).then(function (){
|
||||
return downloadFile(row.fileId)
|
||||
}).then(res =>{
|
||||
console.log(res)
|
||||
// this.download(data.msg);
|
||||
})
|
||||
},
|
||||
// 表单重置
|
||||
reset() {
|
||||
this.form = {
|
||||
|
|
@ -340,6 +360,7 @@ export default {
|
|||
fileType: null,
|
||||
isPublic: "1",
|
||||
};
|
||||
this.fileForm.url = null
|
||||
this.resetForm("form");
|
||||
},
|
||||
/** 搜索按钮操作 */
|
||||
|
|
@ -399,12 +420,14 @@ export default {
|
|||
updateFile(this.form).then(response => {
|
||||
this.msgSuccess("修改成功");
|
||||
this.fileForm.open = false;
|
||||
this.changeArray()
|
||||
this.getList();
|
||||
});
|
||||
} else {
|
||||
addFile(this.form).then(response => {
|
||||
this.msgSuccess("新增成功");
|
||||
this.fileForm.open = false;
|
||||
this.changeArray()
|
||||
this.getList();
|
||||
});
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,6 +1,9 @@
|
|||
package com.hchyun.web.controller.system;
|
||||
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.IOException;
|
||||
import java.io.Serializable;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
|
|
@ -8,9 +11,13 @@ import java.util.Map;
|
|||
import com.hchyun.common.constant.ReturnConstants;
|
||||
import com.hchyun.common.core.controller.HcyBaseController;
|
||||
import com.hchyun.common.utils.ServerResult;
|
||||
import com.hchyun.system.dto.FileDownload;
|
||||
import com.hchyun.system.entity.SysFile;
|
||||
import com.hchyun.system.utils.FtpUtils;
|
||||
import io.swagger.annotations.Api;
|
||||
import io.swagger.annotations.ApiImplicitParam;
|
||||
import io.swagger.annotations.ApiOperation;
|
||||
import org.apache.commons.io.IOUtils;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.security.access.prepost.PreAuthorize;
|
||||
|
|
@ -27,10 +34,12 @@ import org.springframework.web.bind.annotation.RestController;
|
|||
import com.hchyun.common.annotation.Log;
|
||||
import com.hchyun.common.core.entity.AjaxResult;
|
||||
import com.hchyun.common.enums.BusinessType;
|
||||
import com.hchyun.system.entity.File;
|
||||
import com.hchyun.system.service.FileService;
|
||||
import com.hchyun.common.utils.poi.ExcelUtil;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
/**
|
||||
* 文件信息Controller
|
||||
*
|
||||
|
|
@ -50,13 +59,12 @@ public class FileController extends HcyBaseController {
|
|||
/**
|
||||
* 查询文件信息列表
|
||||
*/
|
||||
@ApiOperation("查询文件信息列表")
|
||||
@PreAuthorize("@ss.hasPermi('system:file:list')")
|
||||
@PutMapping("/list")
|
||||
public Serializable list(@Validated @RequestBody File file) {
|
||||
public Serializable list(@Validated @RequestBody SysFile sysFile) {
|
||||
try {
|
||||
startPage(file.getParams());
|
||||
ServerResult<List<File>> serverResult = fileService.selectFileList(file);
|
||||
startPage(sysFile.getParams());
|
||||
ServerResult<List<SysFile>> serverResult = fileService.selectFileList(sysFile);
|
||||
if (serverResult.isStart()) {
|
||||
return getDataTable(serverResult.getData());
|
||||
} else {
|
||||
|
|
@ -71,14 +79,13 @@ public class FileController extends HcyBaseController {
|
|||
/**
|
||||
* 导出文件信息列表
|
||||
*/
|
||||
@ApiOperation("导出文件信息列表")
|
||||
@PreAuthorize("@ss.hasPermi('system:file:export')")
|
||||
@Log(title = "文件信息", businessType = BusinessType.EXPORT)
|
||||
@PutMapping("/export")
|
||||
public AjaxResult export(@Validated @RequestBody File file) {
|
||||
public AjaxResult export(@Validated @RequestBody SysFile sysFile) {
|
||||
try {
|
||||
ServerResult<List<File>> serverResult = fileService.selectFileList(file);
|
||||
ExcelUtil<File> util = new ExcelUtil<File>(File. class);
|
||||
ServerResult<List<SysFile>> serverResult = fileService.selectFileList(sysFile);
|
||||
ExcelUtil<SysFile> util = new ExcelUtil<SysFile>(SysFile.class);
|
||||
if (serverResult.isStart()) {
|
||||
return util.exportExcel(serverResult.getData(), "file");
|
||||
} else {
|
||||
|
|
@ -93,13 +100,11 @@ public class FileController extends HcyBaseController {
|
|||
/**
|
||||
* 获取文件信息详细信息
|
||||
*/
|
||||
@ApiOperation("获取文件信息详细信息")
|
||||
@ApiImplicitParam(name = "fileId" , value = "文件信息fileId" , required = true, dataType = "Long" , paramType = "path")
|
||||
@PreAuthorize("@ss.hasPermi('system:file:query')")
|
||||
@GetMapping(value = "/{fileId}")
|
||||
public AjaxResult getInfo(@PathVariable("fileId") Long fileId) {
|
||||
try {
|
||||
ServerResult<File> serverResult = fileService.selectFileById(fileId);
|
||||
ServerResult<SysFile> serverResult = fileService.selectFileById(fileId);
|
||||
if (serverResult.isStart()) {
|
||||
return AjaxResult.success(serverResult.getData());
|
||||
} else {
|
||||
|
|
@ -114,16 +119,21 @@ public class FileController extends HcyBaseController {
|
|||
/**
|
||||
* 新增文件信息
|
||||
*/
|
||||
@ApiOperation("新增文件信息")
|
||||
@ApiImplicitParam(name = "file" , value = "新增文件信息信息" , dataType = "File")
|
||||
@PreAuthorize("@ss.hasPermi('system:file:add')")
|
||||
@Log(title = "文件信息", businessType = BusinessType.INSERT)
|
||||
@PostMapping
|
||||
public AjaxResult add(@RequestBody File file) {
|
||||
public AjaxResult add(@RequestBody SysFile sysFile) {
|
||||
try {
|
||||
ServerResult<Integer> serverResult = fileService.insertFile(file);
|
||||
Map<String, String> modeMap = new HashMap<>();
|
||||
ServerResult<SysFile> serverResult = fileService.insertFile(sysFile);
|
||||
if (serverResult.isStart()) {
|
||||
return AjaxResult.success();
|
||||
if (sysFile.getIsPublic().equals("1")) {
|
||||
String fileUrl = FtpUtils.getResources() + sysFile.getFileAddr().substring(FtpUtils.getPubfiles().length()) + "/" + sysFile.getMapping();
|
||||
modeMap.put("url", fileUrl);
|
||||
}
|
||||
String filrUri = sysFile.getFileAddr() + "/" + sysFile.getMapping();
|
||||
modeMap.put("uri", filrUri);
|
||||
return AjaxResult.success(modeMap);
|
||||
} else {
|
||||
return AjaxResult.error(serverResult.getMsg());
|
||||
}
|
||||
|
|
@ -136,15 +146,13 @@ public class FileController extends HcyBaseController {
|
|||
/**
|
||||
* 修改文件信息
|
||||
*/
|
||||
@ApiOperation("修改文件信息")
|
||||
@ApiImplicitParam(name = "file" , value = "修改文件信息信息" , dataType = "File")
|
||||
@PreAuthorize("@ss.hasPermi('system:file:edit')")
|
||||
@Log(title = "文件信息", businessType = BusinessType.UPDATE)
|
||||
@PutMapping
|
||||
public AjaxResult edit(@RequestBody File file) {
|
||||
public AjaxResult edit(@RequestBody SysFile sysFile) {
|
||||
try {
|
||||
|
||||
ServerResult<Integer> serverResult = fileService.updateFile(file);
|
||||
ServerResult<Integer> serverResult = fileService.updateFile(sysFile);
|
||||
if (serverResult.isStart()) {
|
||||
return AjaxResult.success();
|
||||
} else {
|
||||
|
|
@ -159,8 +167,6 @@ public class FileController extends HcyBaseController {
|
|||
/**
|
||||
* 删除文件信息
|
||||
*/
|
||||
@ApiOperation("删除文件信息")
|
||||
@ApiImplicitParam(name = "fileIds" , value = "文件信息fileIds" , required = true, dataType = "Long" , paramType = "path")
|
||||
@PreAuthorize("@ss.hasPermi('system:file:remove')")
|
||||
@Log(title = "文件信息", businessType = BusinessType.DELETE)
|
||||
@DeleteMapping("/{fileIds}")
|
||||
|
|
@ -180,4 +186,19 @@ public class FileController extends HcyBaseController {
|
|||
return AjaxResult.error(ReturnConstants.SYS_ERROR);
|
||||
}
|
||||
}
|
||||
|
||||
@GetMapping("/download/{fileId}")
|
||||
public AjaxResult download(@PathVariable Long fileId, HttpServletRequest request, HttpServletResponse response) {
|
||||
try {
|
||||
ServerResult<SysFile> serverResult = fileService.downloadFile(fileId);
|
||||
if (serverResult.isStart()){
|
||||
return AjaxResult.success(serverResult.getData().getFileName());
|
||||
}else {
|
||||
return AjaxResult.error(serverResult.getMsg());
|
||||
}
|
||||
} catch (RuntimeException e) {
|
||||
logger.error(e.getMessage());
|
||||
return AjaxResult.error(ReturnConstants.SYS_ERROR);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -14,6 +14,25 @@ hchyun:
|
|||
addressEnabled: false
|
||||
# 验证码类型 math 数组计算 char 字符验证
|
||||
captchaType: math
|
||||
ftp:
|
||||
# ftp服务器ip地址
|
||||
ftpAddress: 114.215.82.135
|
||||
# 端口号
|
||||
ftpPort: 21
|
||||
# 用户名
|
||||
ftpUsername: hcyftp
|
||||
# 密码
|
||||
ftpPassword: hcyftp@2020
|
||||
# 字符集编码
|
||||
encoding: UTF-8
|
||||
# 资源域名末尾以/结尾
|
||||
resources: http://download.hchyun.com/
|
||||
# 公开目录
|
||||
pubfiles: pubfiles
|
||||
# 保护目录
|
||||
prifiles: prifiles
|
||||
|
||||
|
||||
|
||||
# 开发环境配置
|
||||
server:
|
||||
|
|
|
|||
|
|
@ -17,6 +17,13 @@
|
|||
|
||||
<dependencies>
|
||||
|
||||
<!-- FTP上传文件 -->
|
||||
<dependency>
|
||||
<groupId>commons-net</groupId>
|
||||
<artifactId>commons-net</artifactId>
|
||||
<version>3.7</version>
|
||||
</dependency>
|
||||
|
||||
<!-- Spring框架基本的核心工具 -->
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
|
|
|
|||
|
|
@ -10,107 +10,106 @@ import org.springframework.stereotype.Component;
|
|||
*/
|
||||
@Component
|
||||
@ConfigurationProperties(prefix = "hchyun")
|
||||
public class HchYunConfig
|
||||
{
|
||||
/** 项目名称 */
|
||||
//@ConfigurationProperties(prefix = "ftp.service")
|
||||
public class HchYunConfig {
|
||||
/**
|
||||
* 项目名称
|
||||
*/
|
||||
private String name;
|
||||
|
||||
/** 版本 */
|
||||
/**
|
||||
* 版本
|
||||
*/
|
||||
private String version;
|
||||
|
||||
/** 版权年份 */
|
||||
/**
|
||||
* 版权年份
|
||||
*/
|
||||
private String copyrightYear;
|
||||
|
||||
/** 实例演示开关 */
|
||||
/**
|
||||
* 实例演示开关
|
||||
*/
|
||||
private boolean demoEnabled;
|
||||
|
||||
/** 上传路径 */
|
||||
/**
|
||||
* 上传路径
|
||||
*/
|
||||
private static String profile;
|
||||
|
||||
/** 获取地址开关 */
|
||||
/**
|
||||
* 获取地址开关
|
||||
*/
|
||||
private static boolean addressEnabled;
|
||||
|
||||
public String getName()
|
||||
{
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name)
|
||||
{
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getVersion()
|
||||
{
|
||||
public String getVersion() {
|
||||
return version;
|
||||
}
|
||||
|
||||
public void setVersion(String version)
|
||||
{
|
||||
public void setVersion(String version) {
|
||||
this.version = version;
|
||||
}
|
||||
|
||||
public String getCopyrightYear()
|
||||
{
|
||||
public String getCopyrightYear() {
|
||||
return copyrightYear;
|
||||
}
|
||||
|
||||
public void setCopyrightYear(String copyrightYear)
|
||||
{
|
||||
public void setCopyrightYear(String copyrightYear) {
|
||||
this.copyrightYear = copyrightYear;
|
||||
}
|
||||
|
||||
public boolean isDemoEnabled()
|
||||
{
|
||||
public boolean isDemoEnabled() {
|
||||
return demoEnabled;
|
||||
}
|
||||
|
||||
public void setDemoEnabled(boolean demoEnabled)
|
||||
{
|
||||
public void setDemoEnabled(boolean demoEnabled) {
|
||||
this.demoEnabled = demoEnabled;
|
||||
}
|
||||
|
||||
public static String getProfile()
|
||||
{
|
||||
public static String getProfile() {
|
||||
return profile;
|
||||
}
|
||||
|
||||
public void setProfile(String profile)
|
||||
{
|
||||
public void setProfile(String profile) {
|
||||
HchYunConfig.profile = profile;
|
||||
}
|
||||
|
||||
public static boolean isAddressEnabled()
|
||||
{
|
||||
public static boolean isAddressEnabled() {
|
||||
return addressEnabled;
|
||||
}
|
||||
|
||||
public void setAddressEnabled(boolean addressEnabled)
|
||||
{
|
||||
public void setAddressEnabled(boolean addressEnabled) {
|
||||
HchYunConfig.addressEnabled = addressEnabled;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* 获取头像上传路径
|
||||
*/
|
||||
public static String getAvatarPath()
|
||||
{
|
||||
public static String getAvatarPath() {
|
||||
return getProfile() + "/avatar";
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取下载路径
|
||||
*/
|
||||
public static String getDownloadPath()
|
||||
{
|
||||
public static String getDownloadPath() {
|
||||
return getProfile() + "/download/";
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取上传路径
|
||||
*/
|
||||
public static String getUploadPath()
|
||||
{
|
||||
public static String getUploadPath() {
|
||||
return getProfile() + "/upload";
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -14,8 +14,7 @@ import javax.servlet.http.HttpServletRequest;
|
|||
*
|
||||
* @author hchyun
|
||||
*/
|
||||
public class FileUtils extends org.apache.commons.io.FileUtils
|
||||
{
|
||||
public class FileUtils extends org.apache.commons.io.FileUtils {
|
||||
public static String FILENAME_PATTERN = "[a-zA-Z0-9_\\-\\|\\.\\u4e00-\\u9fa5]+";
|
||||
|
||||
/**
|
||||
|
|
@ -25,49 +24,33 @@ public class FileUtils extends org.apache.commons.io.FileUtils
|
|||
* @param os 输出流
|
||||
* @return
|
||||
*/
|
||||
public static void writeBytes(String filePath, OutputStream os) throws IOException
|
||||
{
|
||||
public static void writeBytes(String filePath, OutputStream os) throws IOException {
|
||||
FileInputStream fis = null;
|
||||
try
|
||||
{
|
||||
try {
|
||||
File file = new File(filePath);
|
||||
if (!file.exists())
|
||||
{
|
||||
if (!file.exists()) {
|
||||
throw new FileNotFoundException(filePath);
|
||||
}
|
||||
fis = new FileInputStream(file);
|
||||
byte[] b = new byte[1024];
|
||||
int length;
|
||||
while ((length = fis.read(b)) > 0)
|
||||
{
|
||||
while ((length = fis.read(b)) > 0) {
|
||||
os.write(b, 0, length);
|
||||
}
|
||||
}
|
||||
catch (IOException e)
|
||||
{
|
||||
} catch (IOException e) {
|
||||
throw e;
|
||||
}
|
||||
finally
|
||||
{
|
||||
if (os != null)
|
||||
{
|
||||
try
|
||||
{
|
||||
} finally {
|
||||
if (os != null) {
|
||||
try {
|
||||
os.close();
|
||||
}
|
||||
catch (IOException e1)
|
||||
{
|
||||
} catch (IOException e1) {
|
||||
e1.printStackTrace();
|
||||
}
|
||||
}
|
||||
if (fis != null)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (fis != null) {
|
||||
try {
|
||||
fis.close();
|
||||
}
|
||||
catch (IOException e1)
|
||||
{
|
||||
} catch (IOException e1) {
|
||||
e1.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
|
@ -80,13 +63,11 @@ public class FileUtils extends org.apache.commons.io.FileUtils
|
|||
* @param filePath 文件
|
||||
* @return
|
||||
*/
|
||||
public static boolean deleteFile(String filePath)
|
||||
{
|
||||
public static boolean deleteFile(String filePath) {
|
||||
boolean flag = false;
|
||||
File file = new File(filePath);
|
||||
// 路径为文件且不为空则进行删除
|
||||
if (file.isFile() && file.exists())
|
||||
{
|
||||
if (file.isFile() && file.exists()) {
|
||||
file.delete();
|
||||
flag = true;
|
||||
}
|
||||
|
|
@ -99,8 +80,7 @@ public class FileUtils extends org.apache.commons.io.FileUtils
|
|||
* @param filename 文件名称
|
||||
* @return true 正常 false 非法
|
||||
*/
|
||||
public static boolean isValidFilename(String filename)
|
||||
{
|
||||
public static boolean isValidFilename(String filename) {
|
||||
return filename.matches(FILENAME_PATTERN);
|
||||
}
|
||||
|
||||
|
|
@ -112,28 +92,20 @@ public class FileUtils extends org.apache.commons.io.FileUtils
|
|||
* @return 编码后的文件名
|
||||
*/
|
||||
public static String setFileDownloadHeader(HttpServletRequest request, String fileName)
|
||||
throws UnsupportedEncodingException
|
||||
{
|
||||
throws UnsupportedEncodingException {
|
||||
final String agent = request.getHeader("USER-AGENT");
|
||||
String filename = fileName;
|
||||
if (agent.contains("MSIE"))
|
||||
{
|
||||
if (agent.contains("MSIE")) {
|
||||
// IE浏览器
|
||||
filename = URLEncoder.encode(filename, "utf-8");
|
||||
filename = filename.replace("+", " ");
|
||||
}
|
||||
else if (agent.contains("Firefox"))
|
||||
{
|
||||
} else if (agent.contains("Firefox")) {
|
||||
// 火狐浏览器
|
||||
filename = new String(fileName.getBytes(), "ISO8859-1");
|
||||
}
|
||||
else if (agent.contains("Chrome"))
|
||||
{
|
||||
} else if (agent.contains("Chrome")) {
|
||||
// google浏览器
|
||||
filename = URLEncoder.encode(filename, "utf-8");
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
// 其它浏览器
|
||||
filename = URLEncoder.encode(filename, "utf-8");
|
||||
}
|
||||
|
|
|
|||
|
|
@ -13,8 +13,7 @@ import com.hchyun.common.utils.http.HttpUtils;
|
|||
*
|
||||
* @author hchyun
|
||||
*/
|
||||
public class AddressUtils
|
||||
{
|
||||
public class AddressUtils {
|
||||
private static final Logger log = LoggerFactory.getLogger(AddressUtils.class);
|
||||
|
||||
// IP地址查询
|
||||
|
|
@ -23,21 +22,16 @@ public class AddressUtils
|
|||
// 未知地址
|
||||
public static final String UNKNOWN = "XX XX";
|
||||
|
||||
public static String getRealAddressByIP(String ip)
|
||||
{
|
||||
public static String getRealAddressByIP(String ip) {
|
||||
String address = UNKNOWN;
|
||||
// 内网不查询
|
||||
if (IpUtils.internalIp(ip))
|
||||
{
|
||||
if (IpUtils.internalIp(ip)) {
|
||||
return "内网IP";
|
||||
}
|
||||
if (HchYunConfig.isAddressEnabled())
|
||||
{
|
||||
try
|
||||
{
|
||||
if (HchYunConfig.isAddressEnabled()) {
|
||||
try {
|
||||
String rspStr = HttpUtils.sendGet(IP_URL, "ip=" + ip + "&json=true" , Constants.GBK);
|
||||
if (StringUtils.isEmpty(rspStr))
|
||||
{
|
||||
if (StringUtils.isEmpty(rspStr)) {
|
||||
log.error("获取地理位置异常 {}" , ip);
|
||||
return UNKNOWN;
|
||||
}
|
||||
|
|
@ -45,9 +39,7 @@ public class AddressUtils
|
|||
String region = obj.getString("pro");
|
||||
String city = obj.getString("city");
|
||||
return String.format("%s %s" , region, city);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
} catch (Exception e) {
|
||||
log.error("获取地理位置异常 {}" , ip);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -111,6 +111,7 @@ public class SecurityConfig extends WebSecurityConfigurerAdapter {
|
|||
.antMatchers("/*/api-docs").anonymous()
|
||||
.antMatchers("/druid/**").anonymous()
|
||||
.antMatchers("/tests/**").anonymous()
|
||||
.antMatchers("/system/file/download/**").anonymous()
|
||||
// 除上面外的所有请求全部需要鉴权认证
|
||||
.anyRequest().authenticated()
|
||||
.and()
|
||||
|
|
|
|||
|
|
@ -23,16 +23,14 @@ import com.hchyun.common.utils.StringUtils;
|
|||
* @author hchyun
|
||||
*/
|
||||
@RestControllerAdvice
|
||||
public class GlobalExceptionHandler
|
||||
{
|
||||
public class GlobalExceptionHandler {
|
||||
private static final Logger log = LoggerFactory.getLogger(GlobalExceptionHandler.class);
|
||||
|
||||
/**
|
||||
* 基础异常
|
||||
*/
|
||||
@ExceptionHandler(BaseException.class)
|
||||
public AjaxResult baseException(BaseException e)
|
||||
{
|
||||
public AjaxResult baseException(BaseException e) {
|
||||
return AjaxResult.error(e.getMessage());
|
||||
}
|
||||
|
||||
|
|
@ -40,46 +38,39 @@ public class GlobalExceptionHandler
|
|||
* 业务异常
|
||||
*/
|
||||
@ExceptionHandler(CustomException.class)
|
||||
public AjaxResult businessException(CustomException e)
|
||||
{
|
||||
if (StringUtils.isNull(e.getCode()))
|
||||
{
|
||||
public AjaxResult businessException(CustomException e) {
|
||||
if (StringUtils.isNull(e.getCode())) {
|
||||
return AjaxResult.error(e.getMessage());
|
||||
}
|
||||
return AjaxResult.error(e.getCode(), e.getMessage());
|
||||
}
|
||||
|
||||
@ExceptionHandler(NoHandlerFoundException.class)
|
||||
public AjaxResult handlerNoFoundException(Exception e)
|
||||
{
|
||||
public AjaxResult handlerNoFoundException(Exception e) {
|
||||
log.error(e.getMessage(), e);
|
||||
return AjaxResult.error(HttpStatus.NOT_FOUND, "路径不存在,请检查路径是否正确");
|
||||
}
|
||||
|
||||
@ExceptionHandler(AccessDeniedException.class)
|
||||
public AjaxResult handleAuthorizationException(AccessDeniedException e)
|
||||
{
|
||||
public AjaxResult handleAuthorizationException(AccessDeniedException e) {
|
||||
log.error(e.getMessage());
|
||||
return AjaxResult.error(HttpStatus.FORBIDDEN, "没有权限,请联系管理员授权");
|
||||
}
|
||||
|
||||
@ExceptionHandler(AccountExpiredException.class)
|
||||
public AjaxResult handleAccountExpiredException(AccountExpiredException e)
|
||||
{
|
||||
public AjaxResult handleAccountExpiredException(AccountExpiredException e) {
|
||||
log.error(e.getMessage(), e);
|
||||
return AjaxResult.error(e.getMessage());
|
||||
}
|
||||
|
||||
@ExceptionHandler(UsernameNotFoundException.class)
|
||||
public AjaxResult handleUsernameNotFoundException(UsernameNotFoundException e)
|
||||
{
|
||||
public AjaxResult handleUsernameNotFoundException(UsernameNotFoundException e) {
|
||||
log.error(e.getMessage(), e);
|
||||
return AjaxResult.error(e.getMessage());
|
||||
}
|
||||
|
||||
@ExceptionHandler(Exception.class)
|
||||
public AjaxResult handleException(Exception e)
|
||||
{
|
||||
public AjaxResult handleException(Exception e) {
|
||||
log.error(e.getMessage(), e);
|
||||
return AjaxResult.error(e.getMessage());
|
||||
}
|
||||
|
|
@ -88,8 +79,7 @@ public class GlobalExceptionHandler
|
|||
* 自定义验证异常
|
||||
*/
|
||||
@ExceptionHandler(BindException.class)
|
||||
public AjaxResult validatedBindException(BindException e)
|
||||
{
|
||||
public AjaxResult validatedBindException(BindException e) {
|
||||
log.error(e.getMessage(), e);
|
||||
String message = e.getAllErrors().get(0).getDefaultMessage();
|
||||
return AjaxResult.error(message);
|
||||
|
|
@ -99,8 +89,7 @@ public class GlobalExceptionHandler
|
|||
* 自定义验证异常
|
||||
*/
|
||||
@ExceptionHandler(MethodArgumentNotValidException.class)
|
||||
public Object validExceptionHandler(MethodArgumentNotValidException e)
|
||||
{
|
||||
public Object validExceptionHandler(MethodArgumentNotValidException e) {
|
||||
log.error(e.getMessage(), e);
|
||||
String message = e.getBindingResult().getFieldError().getDefaultMessage();
|
||||
return AjaxResult.error(message);
|
||||
|
|
@ -110,8 +99,7 @@ public class GlobalExceptionHandler
|
|||
* 演示模式异常
|
||||
*/
|
||||
@ExceptionHandler(DemoModeException.class)
|
||||
public AjaxResult demoModeException(DemoModeException e)
|
||||
{
|
||||
public AjaxResult demoModeException(DemoModeException e) {
|
||||
return AjaxResult.error("演示模式,不允许操作");
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,9 +1,8 @@
|
|||
package com.hchyun.system.dao;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import com.hchyun.system.entity.File;
|
||||
import com.hchyun.system.entity.SysFile;
|
||||
|
||||
/**
|
||||
* 文件信息Mapper接口
|
||||
|
|
@ -18,31 +17,31 @@ public interface FileDao {
|
|||
* @param fileId 文件信息ID
|
||||
* @return 文件信息
|
||||
*/
|
||||
File selectFileById(Long fileId);
|
||||
SysFile selectFileById(Long fileId);
|
||||
|
||||
/**
|
||||
* 查询文件信息列表
|
||||
*
|
||||
* @param file 文件信息
|
||||
* @param sysFile 文件信息
|
||||
* @return 文件信息集合
|
||||
*/
|
||||
List<File> selectFileList(File file);
|
||||
List<SysFile> selectFileList(SysFile sysFile);
|
||||
|
||||
/**
|
||||
* 新增文件信息
|
||||
*
|
||||
* @param file 文件信息
|
||||
* @param sysFile 文件信息
|
||||
* @return 结果
|
||||
*/
|
||||
int insertFile(File file);
|
||||
int insertFile(SysFile sysFile);
|
||||
|
||||
/**
|
||||
* 修改文件信息
|
||||
*
|
||||
* @param file 文件信息
|
||||
* @param sysFile 文件信息
|
||||
* @return 结果
|
||||
*/
|
||||
int updateFile(File file);
|
||||
int updateFile(SysFile sysFile);
|
||||
|
||||
/**
|
||||
* 删除文件信息
|
||||
|
|
|
|||
|
|
@ -0,0 +1,29 @@
|
|||
package com.hchyun.system.dto;
|
||||
|
||||
import java.io.InputStream;
|
||||
|
||||
public class FileDownload {
|
||||
private String fileName;
|
||||
private InputStream inputStream;
|
||||
|
||||
public FileDownload(String fileName, InputStream inputStream) {
|
||||
this.fileName = fileName;
|
||||
this.inputStream = inputStream;
|
||||
}
|
||||
|
||||
public String getFileName() {
|
||||
return fileName;
|
||||
}
|
||||
|
||||
public void setFileName(String fileName) {
|
||||
this.fileName = fileName;
|
||||
}
|
||||
|
||||
public InputStream getInputStream() {
|
||||
return inputStream;
|
||||
}
|
||||
|
||||
public void setInputStream(InputStream inputStream) {
|
||||
this.inputStream = inputStream;
|
||||
}
|
||||
}
|
||||
|
|
@ -14,7 +14,7 @@ import org.apache.commons.lang3.builder.ToStringStyle;
|
|||
* @date 2021-02-17
|
||||
*/
|
||||
@ApiModel("文件信息")
|
||||
public class File extends BaseEntity {
|
||||
public class SysFile extends BaseEntity {
|
||||
|
||||
/**
|
||||
* 文件id
|
||||
|
|
@ -59,7 +59,7 @@ public class File extends BaseEntity {
|
|||
*/
|
||||
@Excel(name = "文件大小(MB)")
|
||||
@ApiModelProperty("文件大小(MB)")
|
||||
private String fileSize;
|
||||
private Long fileSize;
|
||||
/**
|
||||
* 是否公开
|
||||
*/
|
||||
|
|
@ -68,6 +68,7 @@ public class File extends BaseEntity {
|
|||
private String isPublic;
|
||||
|
||||
|
||||
|
||||
public void setFileId(Long fileId) {
|
||||
this.fileId = fileId;
|
||||
}
|
||||
|
|
@ -124,12 +125,12 @@ public class File extends BaseEntity {
|
|||
return fileType;
|
||||
}
|
||||
|
||||
public void setFileSize(String fileSize) {
|
||||
this.fileSize = fileSize;
|
||||
public Long getFileSize() {
|
||||
return fileSize;
|
||||
}
|
||||
|
||||
public String getFileSize() {
|
||||
return fileSize;
|
||||
public void setFileSize(Long fileSize) {
|
||||
this.fileSize = fileSize;
|
||||
}
|
||||
|
||||
public void setIsPublic(String isPublic) {
|
||||
|
|
@ -1,10 +1,9 @@
|
|||
package com.hchyun.system.service;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import com.hchyun.common.utils.ServerResult;
|
||||
import com.hchyun.system.entity.File;
|
||||
import com.hchyun.system.entity.SysFile;
|
||||
|
||||
/**
|
||||
* 文件信息Service接口
|
||||
|
|
@ -19,31 +18,31 @@ public interface FileService {
|
|||
* @param fileId 文件信息ID
|
||||
* @return 文件信息
|
||||
*/
|
||||
ServerResult<File> selectFileById(Long fileId);
|
||||
ServerResult<SysFile> selectFileById(Long fileId);
|
||||
|
||||
/**
|
||||
* 查询文件信息列表
|
||||
*
|
||||
* @param file 文件信息
|
||||
* @param sysFile 文件信息
|
||||
* @return 文件信息集合
|
||||
*/
|
||||
ServerResult<List<File>> selectFileList(File file);
|
||||
ServerResult<List<SysFile>> selectFileList(SysFile sysFile);
|
||||
|
||||
/**
|
||||
* 新增文件信息
|
||||
*
|
||||
* @param file 文件信息
|
||||
* @param sysFile 文件信息
|
||||
* @return 结果
|
||||
*/
|
||||
ServerResult<Integer> insertFile(File file);
|
||||
ServerResult<SysFile> insertFile(SysFile sysFile);
|
||||
|
||||
/**
|
||||
* 修改文件信息
|
||||
*
|
||||
* @param file 文件信息
|
||||
* @param sysFile 文件信息
|
||||
* @return 结果
|
||||
*/
|
||||
ServerResult<Integer> updateFile(File file);
|
||||
ServerResult<Integer> updateFile(SysFile sysFile);
|
||||
|
||||
/**
|
||||
* 批量删除文件信息
|
||||
|
|
@ -61,4 +60,11 @@ public interface FileService {
|
|||
*/
|
||||
ServerResult<Integer> deleteFileById(Long fileId);
|
||||
|
||||
/**
|
||||
* 下载指定文件
|
||||
* @param fileId
|
||||
* @return
|
||||
*/
|
||||
ServerResult<SysFile> downloadFile(Long fileId);
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,20 +1,18 @@
|
|||
package com.hchyun.system.service.impl;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import com.hchyun.common.constant.ReturnConstants;
|
||||
import com.hchyun.common.utils.SecurityUtils;
|
||||
import com.hchyun.common.utils.DateUtils;
|
||||
import com.hchyun.common.utils.SecurityUtils;
|
||||
import com.hchyun.common.utils.DateUtils;
|
||||
import com.hchyun.common.utils.ServerResult;
|
||||
import com.hchyun.system.entity.SysFile;
|
||||
import com.hchyun.system.utils.FtpUtils;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
import com.hchyun.system.dao.FileDao;
|
||||
import com.hchyun.system.entity.File;
|
||||
import com.hchyun.system.service.FileService;
|
||||
|
||||
/**
|
||||
|
|
@ -37,74 +35,78 @@ public class FileServiceImpl implements FileService {
|
|||
* @return 文件信息
|
||||
*/
|
||||
@Override
|
||||
public ServerResult<File> selectFileById(Long fileId) {
|
||||
public ServerResult<SysFile> selectFileById(Long fileId) {
|
||||
try {
|
||||
File file = fileDao.selectFileById(fileId);
|
||||
if (file != null){
|
||||
return new ServerResult<File>(true,file);
|
||||
SysFile sysFile = fileDao.selectFileById(fileId);
|
||||
if (sysFile != null){
|
||||
return new ServerResult<SysFile>(true, sysFile);
|
||||
}else {
|
||||
return new ServerResult<File>(false, ReturnConstants.RESULT_EMPTY);
|
||||
return new ServerResult<SysFile>(false, ReturnConstants.RESULT_EMPTY);
|
||||
}
|
||||
}catch (RuntimeException e){
|
||||
logger.error(e.getMessage());
|
||||
return new ServerResult<File>(false,ReturnConstants.DB_EX);
|
||||
return new ServerResult<SysFile>(false,ReturnConstants.DB_EX);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 查询文件信息列表
|
||||
*
|
||||
* @param file 文件信息
|
||||
* @param sysFile 文件信息
|
||||
* @return 文件信息
|
||||
*/
|
||||
@Override
|
||||
public ServerResult<List<File>> selectFileList(File file) {
|
||||
public ServerResult<List<SysFile>> selectFileList(SysFile sysFile) {
|
||||
try {
|
||||
List<File> fileList = fileDao.selectFileList(file);
|
||||
if (fileList.size()>0){
|
||||
return new ServerResult<List<File>>(true,fileList);
|
||||
List<SysFile> sysFileList = fileDao.selectFileList(sysFile);
|
||||
if (sysFileList.size()>0){
|
||||
return new ServerResult<List<SysFile>>(true, sysFileList);
|
||||
}else {
|
||||
return new ServerResult<List<File>>(false,ReturnConstants.RESULT_EMPTY);
|
||||
return new ServerResult<List<SysFile>>(false,ReturnConstants.RESULT_EMPTY);
|
||||
}
|
||||
}catch (RuntimeException e){
|
||||
logger.error(e.getMessage());
|
||||
return new ServerResult<List<File>>(false,ReturnConstants.DB_EX);
|
||||
return new ServerResult<List<SysFile>>(false,ReturnConstants.DB_EX);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 新增文件信息
|
||||
*
|
||||
* @param file 文件信息
|
||||
* @param sysFile 文件信息
|
||||
* @return 结果
|
||||
*/
|
||||
@Override
|
||||
public ServerResult<Integer> insertFile(File file) {
|
||||
public ServerResult<SysFile> insertFile(SysFile sysFile) {
|
||||
try {
|
||||
file.setCreateBy(SecurityUtils.getUserId());
|
||||
Integer renewal = fileDao.insertFile(file);
|
||||
sysFile.setCreateBy(SecurityUtils.getUserId());
|
||||
sysFile = FtpUtils.uploadFtp(sysFile);
|
||||
if (sysFile == null){
|
||||
return new ServerResult<>(false,"文件上传失败!");
|
||||
}
|
||||
Integer renewal = fileDao.insertFile(sysFile);
|
||||
if (renewal >0){
|
||||
return new ServerResult<Integer>(true,renewal);
|
||||
return new ServerResult<>(true,sysFile);
|
||||
}else {
|
||||
return new ServerResult<Integer>(false,ReturnConstants.SYS_FAILL);
|
||||
return new ServerResult<>(false,ReturnConstants.SYS_FAILL);
|
||||
}
|
||||
}catch (RuntimeException e){
|
||||
logger.error(e.getMessage());
|
||||
return new ServerResult<Integer>(false,ReturnConstants.DB_EX);
|
||||
return new ServerResult<>(false,ReturnConstants.DB_EX);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 修改文件信息
|
||||
*
|
||||
* @param file 文件信息
|
||||
* @param sysFile 文件信息
|
||||
* @return 结果
|
||||
*/
|
||||
@Override
|
||||
public ServerResult<Integer> updateFile(File file) {
|
||||
public ServerResult<Integer> updateFile(SysFile sysFile) {
|
||||
try {
|
||||
file.setUpdateBy(SecurityUtils.getUserId());
|
||||
Integer renewal = fileDao.updateFile(file);
|
||||
sysFile.setUpdateBy(SecurityUtils.getUserId());
|
||||
Integer renewal = fileDao.updateFile(sysFile);
|
||||
if (renewal >0){
|
||||
return new ServerResult<Integer>(true,renewal);
|
||||
}else {
|
||||
|
|
@ -125,6 +127,15 @@ public class FileServiceImpl implements FileService {
|
|||
@Override
|
||||
public ServerResult<Integer> deleteFileByIds(Long[] fileIds) {
|
||||
try {
|
||||
List<SysFile> sysFiles = new ArrayList<>();
|
||||
SysFile sysFile = null;
|
||||
for (Long fileId : fileIds) {
|
||||
sysFile = fileDao.selectFileById(fileId);
|
||||
sysFiles.add(sysFile);
|
||||
}
|
||||
if (!FtpUtils.deleteFile(sysFiles)){
|
||||
return new ServerResult<>(false,ReturnConstants.OP_ERROR);
|
||||
}
|
||||
Integer renewal = fileDao.deleteFileByIds(fileIds);
|
||||
if (renewal >0){
|
||||
return new ServerResult<Integer>(true,renewal);
|
||||
|
|
@ -137,6 +148,22 @@ public class FileServiceImpl implements FileService {
|
|||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public ServerResult<SysFile> downloadFile(Long fileId) {
|
||||
try {
|
||||
SysFile sysFile = fileDao.selectFileById(fileId);
|
||||
String fileName = FtpUtils.downloadFile(sysFile);
|
||||
if (fileName==null){
|
||||
return new ServerResult<>(false,"文件下载失败!");
|
||||
}else {
|
||||
return new ServerResult<>(true,sysFile);
|
||||
}
|
||||
}catch (RuntimeException e){
|
||||
logger.error(e.getMessage());
|
||||
return new ServerResult<>(false,ReturnConstants.DB_EX);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除文件信息信息
|
||||
*
|
||||
|
|
|
|||
|
|
@ -0,0 +1,278 @@
|
|||
package com.hchyun.system.utils;
|
||||
|
||||
|
||||
import com.hchyun.common.config.HchYunConfig;
|
||||
import com.hchyun.common.utils.DateUtils;
|
||||
import com.hchyun.common.utils.file.FileUtils;
|
||||
import com.hchyun.common.utils.uuid.IdUtils;
|
||||
import com.hchyun.system.dto.FileDownload;
|
||||
import com.hchyun.system.entity.SysFile;
|
||||
import org.apache.commons.io.FilenameUtils;
|
||||
import org.apache.commons.net.ftp.FTPClient;
|
||||
import org.apache.commons.net.ftp.FTPClientConfig;
|
||||
import org.apache.commons.net.ftp.FTPReply;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.io.*;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @Author 18209
|
||||
* @Date 2021/2/18 15:34
|
||||
* @Version 1.0
|
||||
*/
|
||||
@Component
|
||||
@ConfigurationProperties(prefix = "ftp")
|
||||
public class FtpUtils {
|
||||
|
||||
private static Logger logger = LoggerFactory.getLogger(FtpUtils.class);
|
||||
|
||||
private static String baseDir = "/profile";
|
||||
|
||||
private static String ftpDir = "/profile/upload";
|
||||
|
||||
private static String pubfiles;
|
||||
|
||||
private static String prifiles;
|
||||
|
||||
public static String getPubfiles() {
|
||||
return pubfiles;
|
||||
}
|
||||
|
||||
//ftp服务器ip地址
|
||||
private static String ftpAddress;
|
||||
//端口号
|
||||
private static int ftpPort;
|
||||
//用户名
|
||||
private static String ftpUsername;
|
||||
//密码
|
||||
private static String ftpPassword;
|
||||
//字符集编码
|
||||
private static String encoding;
|
||||
//静态资源域名
|
||||
private static String resources;
|
||||
|
||||
|
||||
public void setPubfiles(String pubfiles) {
|
||||
FtpUtils.pubfiles = pubfiles + "/";
|
||||
}
|
||||
|
||||
public void setPrifiles(String prifiles) {
|
||||
FtpUtils.prifiles = prifiles +"/";
|
||||
}
|
||||
|
||||
public static String getResources() {
|
||||
return resources;
|
||||
}
|
||||
|
||||
public void setResources(String resources) {
|
||||
FtpUtils.resources = resources;
|
||||
}
|
||||
|
||||
public void setFtpAddress(String ftpAddress) {
|
||||
FtpUtils.ftpAddress = ftpAddress;
|
||||
}
|
||||
|
||||
public void setFtpPort(int ftpPort) {
|
||||
FtpUtils.ftpPort = ftpPort;
|
||||
}
|
||||
|
||||
public void setFtpUsername(String ftpUsername) {
|
||||
FtpUtils.ftpUsername = ftpUsername;
|
||||
}
|
||||
|
||||
public void setFtpPassword(String ftpPassword) {
|
||||
FtpUtils.ftpPassword = ftpPassword;
|
||||
}
|
||||
|
||||
public void setEncoding(String encoding) {
|
||||
FtpUtils.encoding = encoding;
|
||||
}
|
||||
|
||||
/**
|
||||
* 上传文件
|
||||
* @param sysFile
|
||||
* @return
|
||||
*/
|
||||
public static SysFile uploadFtp(SysFile sysFile) {
|
||||
String dir = HchYunConfig.getProfile() + sysFile.getFileAddr().substring(baseDir.length());
|
||||
String ftpdir = "";
|
||||
if (sysFile.getIsPublic().equals("1")) {
|
||||
ftpdir += pubfiles;
|
||||
} else {
|
||||
ftpdir += prifiles;
|
||||
}
|
||||
ftpdir +=DateUtils.datePath();
|
||||
File file = new File(dir);
|
||||
sysFile.setFileName(file.getName());
|
||||
sysFile.setFileSize(file.length() / 1024 / 1024);
|
||||
sysFile.setMapping(IdUtils.fastUUID() +"."+ getExtension(file.getName()));
|
||||
sysFile.setFileAddr(ftpdir);
|
||||
sysFile.setFileType(getExtension(file.getName()));
|
||||
InputStream inputStream = null;
|
||||
try {
|
||||
inputStream = new FileInputStream(file);
|
||||
} catch (FileNotFoundException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
FTPClient ftpClient = linkFtp();
|
||||
if (ftpClient == null) {
|
||||
return null;
|
||||
}
|
||||
try {
|
||||
String[] basePathList = ftpdir.split("/");
|
||||
ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);//设置文件类型
|
||||
for (int i = 0; i < basePathList.length; i++) {
|
||||
if (!ftpClient.changeWorkingDirectory(basePathList[i])) {
|
||||
ftpClient.makeDirectory(basePathList[i]);//创建文件夹
|
||||
ftpClient.changeWorkingDirectory(basePathList[i]);//改变目录
|
||||
}
|
||||
}
|
||||
ftpClient.enterLocalPassiveMode();
|
||||
ftpClient.storeFile(sysFile.getMapping(), inputStream);
|
||||
inputStream.close();
|
||||
ftpClient.logout();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
if (!FileUtils.deleteFile(dir)){
|
||||
return null;
|
||||
}
|
||||
return sysFile;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* ftp下载文件
|
||||
*
|
||||
* @param sysFile
|
||||
* @return
|
||||
*/
|
||||
public static String downloadFile(SysFile sysFile) {
|
||||
String[] basePathList = sysFile.getFileAddr().split("/");
|
||||
FTPClient ftpClient = linkFtp();
|
||||
try {
|
||||
if (ftpClient == null) {
|
||||
return null;
|
||||
}
|
||||
ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);//设置文件类型
|
||||
//设置linux ftp服务器
|
||||
FTPClientConfig conf = new FTPClientConfig( FTPClientConfig.SYST_UNIX);
|
||||
ftpClient.configure(conf);
|
||||
//设置访问被动模式
|
||||
ftpClient.setRemoteVerificationEnabled(false);
|
||||
ftpClient.enterLocalPassiveMode();
|
||||
for (int i = 0; i < basePathList.length; i++) {
|
||||
ftpClient.changeWorkingDirectory(basePathList[i]);//改变目录
|
||||
}
|
||||
// FTPFile[] ftpFiles = ftpClient.listFiles(articleFile.getFileName());
|
||||
// if (ftpFiles == null || ftpFiles.length == 0) {
|
||||
// logger.error("远程文件不存在");
|
||||
// return null;
|
||||
// } else if (ftpFiles.length > 1) {
|
||||
// logger.error("远程文件是文件夹");
|
||||
// return null;
|
||||
// }
|
||||
String fileDir = HchYunConfig.getDownloadPath()+sysFile.getFileName();
|
||||
OutputStream os = new FileOutputStream(fileDir);
|
||||
ftpClient.retrieveFile(sysFile.getMapping(), os);
|
||||
return fileDir;
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
if (ftpClient.isConnected()) {
|
||||
try {
|
||||
ftpClient.disconnect();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* ftp删除文件
|
||||
* @param sysFileList
|
||||
* @return
|
||||
*/
|
||||
public static boolean deleteFile(List<SysFile> sysFileList) {
|
||||
FTPClient ftpClient = linkFtp();
|
||||
boolean start = false;
|
||||
try {
|
||||
if (ftpClient == null) {
|
||||
return false;
|
||||
}
|
||||
for (SysFile sysFile : sysFileList) {
|
||||
String[] basePathList = sysFile.getFileAddr().split("/");
|
||||
ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);//设置文件类型
|
||||
ftpClient.enterLocalPassiveMode();
|
||||
for (int i = 0; i < basePathList.length; i++) {
|
||||
ftpClient.changeWorkingDirectory(basePathList[i]);//改变目录
|
||||
}
|
||||
start = ftpClient.deleteFile(sysFile.getMapping());
|
||||
if (!start){
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
logger.error("ftp删除文件出错:" + e.toString());
|
||||
return false;
|
||||
} finally {
|
||||
if (ftpClient.isConnected()) {
|
||||
try {
|
||||
ftpClient.disconnect();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* ftp服务器连接
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public static FTPClient linkFtp() {
|
||||
FTPClient ftpClient = new FTPClient();//实例FTP客户端
|
||||
ftpClient.enterLocalPassiveMode();
|
||||
ftpClient.setAutodetectUTF8(true);
|
||||
// 保存FTP控制连接使用的字符集,必须在连接前设置
|
||||
ftpClient.setControlEncoding(encoding);
|
||||
try {
|
||||
ftpClient.connect(ftpAddress, ftpPort);//连接FTP服务器
|
||||
ftpClient.login(ftpUsername, ftpPassword);//用户名密码登录
|
||||
if (!FTPReply.isPositiveCompletion(ftpClient.getReplyCode())) {
|
||||
logger.error("未连接到FTP,用户名或密码错误。");
|
||||
ftpClient.disconnect();
|
||||
return null;
|
||||
} else {
|
||||
logger.info("FTP连接成功");
|
||||
return ftpClient;
|
||||
}
|
||||
} catch (IOException e) {
|
||||
logger.error("FTP登录失败" + e.getMessage());
|
||||
e.printStackTrace();
|
||||
try {
|
||||
ftpClient.disconnect();
|
||||
} catch (IOException ioException) {
|
||||
ioException.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public static final String getExtension(String fileName) {
|
||||
return FilenameUtils.getExtension(fileName);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -4,7 +4,7 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
|
|||
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
|
||||
<mapper namespace="com.hchyun.system.dao.FileDao">
|
||||
|
||||
<resultMap type="com.hchyun.system.entity.File" id="FileResult">
|
||||
<resultMap type="com.hchyun.system.entity.SysFile" id="FileResult">
|
||||
<result property="fileId" column="file_id" />
|
||||
<result property="pId" column="p_id" />
|
||||
<result property="roleIds" column="role_ids" />
|
||||
|
|
@ -23,7 +23,7 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
|
|||
<sql id="selectFileVo">
|
||||
select file_id, p_id, role_ids, file_name, mapping, file_addr, file_type, file_size, is_public, create_by, create_time, update_by, update_time from sys_file
|
||||
</sql>
|
||||
<select id="selectFileList" parameterType="File" resultMap="FileResult">
|
||||
<select id="selectFileList" parameterType="SysFile" resultMap="FileResult">
|
||||
<include refid="selectFileVo"/>
|
||||
<where>
|
||||
<if test="fileName != null and fileName != ''"> and file_name like concat('%', #{fileName}, '%')</if>
|
||||
|
|
@ -40,7 +40,7 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
|
|||
</select>
|
||||
|
||||
|
||||
<insert id="insertFile" parameterType="File" useGeneratedKeys="true" keyProperty="fileId">
|
||||
<insert id="insertFile" parameterType="SysFile" useGeneratedKeys="true" keyProperty="fileId">
|
||||
insert into sys_file
|
||||
<trim prefix="(" suffix=")" suffixOverrides=",">
|
||||
<if test="pId != null">p_id,</if>
|
||||
|
|
@ -68,7 +68,7 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
|
|||
</trim>
|
||||
</insert>
|
||||
|
||||
<update id="updateFile" parameterType="File">
|
||||
<update id="updateFile" parameterType="SysFile">
|
||||
update sys_file
|
||||
<trim prefix="SET" suffixOverrides=",">
|
||||
<if test="pId != null">p_id = #{pId},</if>
|
||||
|
|
|
|||
Loading…
Reference in New Issue