@@ -18,6 +18,7 @@ import org.apache.commons.io.FileUtils;
import org.slf4j.Logger;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.util.FileCopyUtils;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartFile;
@@ -36,6 +37,7 @@ import java.io.FileOutputStream;
import java.io.IOException;
import java.io.IOException;
import java.util.HashMap;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map;
/**
/**
@@ -275,19 +277,18 @@ public class AttachController {
//保存处理附件
//保存处理附件
private void localSaveAttach(final AttachInfo attach, final File upload) throws Exception {
private void localSaveAttach(final AttachInfo attach, final File upload) throws Exception {
try {
try {
//一般附件,上传到服务器,并记录附件
//传到ftp服务器上
attachUtil.saveAttach(attach, upload.getPath(), false);
//写入数据库
dbEngine.insertEntity(attach);
// String sql="insert into "+AttachInfo.ENTITY_NAME+" (attach_id,attach_name,attach_suffix,attach_owner_id,attach_path,attach_type,attach_size,attach_is_temp,attach_party_id,attach_time,attach_user_id,attach_count,attach_ref_id)"
//一般附件,上传到服务器,并记录附件
//传到ftp服务器上
attachUtil.saveAttach(attach, upload.getPath(), false);
//写入数据库
dbEngine.insertEntity(attach);
} finally {
} finally {
UtilFile.delFile(upload.getAbsolutePath());
UtilFile.delFile(upload.getAbsolutePath());
}
}
}
}
//将附件上传到ftp服务器上
//附件下载
@RequestMapping(value = "/download", method = {RequestMethod.GET, RequestMethod.POST})
@RequestMapping(value = "/download", method = {RequestMethod.GET, RequestMethod.POST})
public R down(HttpServletRequest request, HttpServletResponse response, @RequestParam(value = "attachId", required = false) Long attachId) throws Exception {
public R down(HttpServletRequest request, HttpServletResponse response, @RequestParam(value = "attachId", required = false) Long attachId) throws Exception {
AttachInfo attach = dbEngine.queryEntity(AttachInfo.class,attachId);
AttachInfo attach = dbEngine.queryEntity(AttachInfo.class,attachId);
@@ -311,29 +312,22 @@ public class AttachController {
}
}
//图片附件显示路径
//图片附件显示路径
@RequestMapping(value = "/showImg", method = {RequestMethod.GET, RequestMethod.POST} )
public R showImg(HttpServletRequest request, HttpServletRe sponse response, @RequestParam(value = "attachId", required = false) Long attachId) throws Exception {
@GetMapping(value = "/showImg" )
public R showImg(HttpServletResponse response, @RequestParam(value = "attachId", required = false) Long attachId) throws Exception {
AttachInfo attach = dbEngine.queryEntity(AttachInfo.class,attachId);
AttachInfo attach = dbEngine.queryEntity(AttachInfo.class,attachId);
if (null == attach) {
if (null == attach) {
return R.error("附件不存在");
return R.error("附件不存在");
}
}
final String remoteFileName = attach.getName();
final String fileName = attach.getName();
final String remotePath = attach.getPath();
try {
attachUtil.downloadAttach(remotePath, remoteFileName, fileName, response);
} catch (Exception e) {
logger.error("附件下载失败",e);
R.error("附件下载失败");
}
// attachUtil.showImg(attach.getPath(),attach.getName(),response);
attachUtil.downloadAttach(attach.getPath(),attach.getName(),attach.getName(),response);
return null;
return null;
}
}
//图片附件显示路径
//图片附件显示路径
@RequestMapping(value = "/showImgThumb", method = {RequestMethod.GET, RequestMethod.POST})
@RequestMapping(value = "/showImgThumb", method = {RequestMethod.GET, RequestMethod.POST})
public R showImgThumb(HttpServletRequest request, HttpServletRe sponse response, @RequestParam(value = "attachId", required = false) Long attachId,
@RequestParam(value = "width", required = false,defaultValue = "1 00") String width,
@RequestParam(value = "heigh", required = false,defaultValue = "100") String heigh ) throws Exception {
public R showImgThumb(HttpServletResponse response, @RequestParam(value = "attachId", required = false) Long attachId,
@RequestParam(value = "width", required = false,defaultValue = "200") String width,
@RequestParam(value = "height", required = false,defaultValue = "120") String height ) throws Exception {
AttachInfo attach = dbEngine.queryEntity(AttachInfo.class,attachId);
AttachInfo attach = dbEngine.queryEntity(AttachInfo.class,attachId);
if (null == attach) {
if (null == attach) {
return R.error("附件不存在");
return R.error("附件不存在");
@@ -346,7 +340,7 @@ public class AttachController {
String lfd = tempPath + "/thumb_" + attach.getId() + attach.getSuffix();
String lfd = tempPath + "/thumb_" + attach.getId() + attach.getSuffix();
attachUtil.downloadAttach(remotePath, remoteFileName, tempPath);
attachUtil.downloadAttach(remotePath, remoteFileName, tempPath);
int w=Integer.valueOf(width);
int w=Integer.valueOf(width);
int h=Integer.valueOf(heigh);
int h=Integer.valueOf(height );
if (w <= 0) w = 100;
if (w <= 0) w = 100;
if (h <= 0) h = 100;
if (h <= 0) h = 100;
getImgThumb(lfo, lfd, w, h);
getImgThumb(lfo, lfd, w, h);
@@ -396,18 +390,16 @@ public class AttachController {
}
}
//删除附件
//删除附件
@Pos tMapping({"/delete"})
public R del(HttpServletRequest request, @RequestParam(value = "attachId", required = false) Long attachId) throws Exception {
@Ge tMapping({"/delete"})
public R del(@RequestParam(value = "attachIds") String attachIds) {
try {
try {
AttachInfo attach = dbEngine.queryEntity(AttachInfo.class,attachId);
if (null == attach) {
return R.error("附件不存在");
}
attachHelper.deleteOneAttach(attach);
List<AttachInfo> attaches = dbEngine.queryWhere(AttachInfo.class, "attach_id in (" + attachIds + ")");
Assert.isTrue(attaches != null && !attaches.isEmpty(), "附件不存在");
attaches.forEach(a -> attachHelper.deleteOneAttach(a));
return R.success("附件删除成功");
return R.success("附件删除成功");
} catch (Exception e) {
} catch (Exception e) {
logger.error("删除附件失败", e);
logger.error("删除附件失败", e);
return R.error("删除附件失败" );
return R.error(e.getMessage() );
}
}
}
}
@@ -424,4 +416,10 @@ public class AttachController {
}
}
}
//图片附件显示路径
@GetMapping(value = "/loadAttachBean")
public R loadAttachBean(@RequestParam(value = "attachIds") String attachIds) throws Exception {
List<AttachInfo> beans = dbEngine.queryWhere(AttachInfo.class, "attach_id in ("+attachIds+")");
return R.success(beans);
}
}