296 lines
10 KiB
Java
296 lines
10 KiB
Java
package com.rzyc.controller;
|
||
|
||
import com.common.utils.RandomNumber;
|
||
import com.common.utils.StringUtils;
|
||
import com.common.utils.model.*;
|
||
import com.common.utils.upload.FileUpload;
|
||
import com.rzyc.bean.file.FileResult;
|
||
import com.rzyc.bean.file.FileResults;
|
||
import com.rzyc.model.Sysdocument;
|
||
import io.swagger.annotations.Api;
|
||
import io.swagger.annotations.ApiImplicitParam;
|
||
import io.swagger.annotations.ApiImplicitParams;
|
||
import io.swagger.annotations.ApiOperation;
|
||
import org.springframework.web.bind.annotation.*;
|
||
import org.springframework.web.multipart.MultipartFile;
|
||
|
||
import java.util.*;
|
||
|
||
@Api(tags = "文件上传")
|
||
@RestController
|
||
@CrossOrigin("*")
|
||
public class UploadController extends com.rzyc.controller.BaseController {
|
||
|
||
private static Map<String,String> mFileTypes = new HashMap<String,String>();
|
||
|
||
static {
|
||
/* 图片文件 start */
|
||
mFileTypes.put("jpg", "1");
|
||
mFileTypes.put("png", "1");
|
||
mFileTypes.put("gif", "1");
|
||
mFileTypes.put("jpeg", "1");
|
||
/* 图片文件 end */
|
||
|
||
/* 文档 start */
|
||
mFileTypes.put("pdf","2");
|
||
mFileTypes.put("xls","2");
|
||
mFileTypes.put("xlsx","2");
|
||
mFileTypes.put("doc","2");
|
||
mFileTypes.put("docx","2");
|
||
mFileTypes.put("wps","2");
|
||
mFileTypes.put("rtf","2");
|
||
mFileTypes.put("txt","2");
|
||
mFileTypes.put("pptx","2");
|
||
mFileTypes.put("ppt","2");
|
||
/* 文档 end */
|
||
|
||
/* 音频 start */
|
||
mFileTypes.put("mp3","3");
|
||
/* 音频 end */
|
||
|
||
/* 视频 start */
|
||
mFileTypes.put("rmvb","4");
|
||
mFileTypes.put("mp4","4");
|
||
mFileTypes.put("avi","4");
|
||
/* 视频 start */
|
||
|
||
}
|
||
|
||
|
||
/**
|
||
* 获取文件头
|
||
* @param src
|
||
* @return
|
||
* @throws Exception
|
||
*/
|
||
public static String bytesToHexString(byte[] src) throws Exception{
|
||
StringBuilder stringBuilder = new StringBuilder();
|
||
if (src == null || src.length <= 0) {
|
||
return null;
|
||
}
|
||
for (int i = 0; i < src.length; i++) {
|
||
int v = src[i] & 0xFF;
|
||
String hv = Integer.toHexString(v);
|
||
if (hv.length() < 2) {
|
||
stringBuilder.append(0);
|
||
}
|
||
stringBuilder.append(hv);
|
||
}
|
||
return stringBuilder.toString();
|
||
}
|
||
|
||
|
||
|
||
|
||
/**
|
||
*
|
||
* @param xxxx
|
||
* @return
|
||
*/
|
||
public static String checkType(String xxxx) {
|
||
|
||
/**
|
||
常用文件的文件头如下:(以前六位为准)
|
||
JPEG (jpg),文件头:FFD8FF
|
||
PNG (png),文件头:89504E47
|
||
GIF (gif),文件头:47494638
|
||
TIFF (tif),文件头:49492A00
|
||
Windows Bitmap (bmp),文件头:424D
|
||
CAD (dwg),文件头:41433130
|
||
Adobe Photoshop (psd),文件头:38425053
|
||
Rich Text Format (rtf),文件头:7B5C727466
|
||
XML (xml),文件头:3C3F786D6C
|
||
HTML (html),文件头:68746D6C3E
|
||
Email [thorough only] (eml),文件头:44656C69766572792D646174653A
|
||
Outlook Express (dbx),文件头:CFAD12FEC5FD746F
|
||
Outlook (pst),文件头:2142444E
|
||
MS Word/Excel (xls.or.doc),文件头:D0CF11E0
|
||
MS Access (mdb),文件头:5374616E64617264204A
|
||
WordPerfect (wpd),文件头:FF575043
|
||
Postscript (eps.or.ps),文件头:252150532D41646F6265
|
||
Adobe Acrobat (pdf),文件头:255044462D312E
|
||
Quicken (qdf),文件头:AC9EBD8F
|
||
Windows Password (pwl),文件头:E3828596
|
||
ZIP Archive (zip),文件头:504B0304
|
||
RAR Archive (rar),文件头:52617221
|
||
Wave (wav),文件头:57415645
|
||
AVI (avi),文件头:41564920
|
||
Real Audio (ram),文件头:2E7261FD
|
||
Real Media (rm),文件头:2E524D46
|
||
MPEG (mpg),文件头:000001BA
|
||
MPEG (mpg),文件头:000001B3
|
||
Quicktime (mov),文件头:6D6F6F76
|
||
Windows Media (asf),文件头:3026B2758E66CF11
|
||
MIDI (mid),文件头:4D546864
|
||
*/
|
||
switch (xxxx) {
|
||
case "FFD8FF": return "jpg";
|
||
case "89504E": return "png";
|
||
case "474946": return "jif";
|
||
|
||
default: return "0000";
|
||
}
|
||
}
|
||
|
||
|
||
/**
|
||
* 新增文件
|
||
* @param otcId
|
||
* @param otcType
|
||
* @param fileUrl
|
||
* @param fileType
|
||
* @throws Exception
|
||
*/
|
||
public Sysdocument addDocument(String otcId, String otcType, String fileUrl, String fileType, String fileName, String userId, String fileSize)throws Exception{
|
||
fileUrl = delServiceFile(fileUrl);
|
||
Sysdocument document = new Sysdocument();
|
||
document.setSysDocumentId(RandomNumber.getUUid());
|
||
document.setRealFileName(fileName);
|
||
document.setFilePath(fileUrl);
|
||
document.setFileType(fileType);
|
||
document.setFileSize(fileSize);
|
||
document.setOTCId(otcId);
|
||
document.setOTCType(otcType);
|
||
document.setCreatedBy(userId);
|
||
document.setModifiedBy(userId);
|
||
document.setCreatedOn(new Date());
|
||
document.setModifiedOn(new Date());
|
||
sysdocumentMapper.insert(document);
|
||
return document;
|
||
}
|
||
|
||
/**
|
||
* 文件上传
|
||
* @param userId
|
||
* @param OTCId
|
||
* @param OTCType
|
||
* @param file
|
||
* @return
|
||
*/
|
||
@ApiOperation(value = "文件上传", notes = "文件上传")
|
||
@ApiImplicitParams({
|
||
@ApiImplicitParam(name = "userId", value = "用户id", required = true, dataType = "string"),
|
||
@ApiImplicitParam(name = "OTCId", value = "目标uuid", required = true, dataType = "string"),
|
||
@ApiImplicitParam(name = "OTCType", value = "文件类型", required = true, dataType = "string"),
|
||
@ApiImplicitParam(name = "trueName", value = "文件名", required = false, dataType = "string")
|
||
})
|
||
@ResponseBody
|
||
@RequestMapping(value = "/upload", method = RequestMethod.POST)
|
||
public FileResult upload(String userId, String OTCId, String OTCType,String trueName, MultipartFile file) {
|
||
FileResult fileResult = new FileResult();
|
||
|
||
Boolean success = false;
|
||
//赋值
|
||
String fileName = trueName;
|
||
try {
|
||
String url = "";
|
||
if (StringUtils.isBlank(fileName)) {
|
||
fileName = file.getOriginalFilename();
|
||
}
|
||
System.out.println("file name is -> "+fileName);
|
||
String suffix = fileName.substring(fileName.lastIndexOf(".")+1,fileName.length());
|
||
suffix = suffix.toLowerCase();
|
||
if(StringUtils.isNotBlank(mFileTypes.get(suffix))){
|
||
url = FileUpload.uploadFile(file.getInputStream(),fileName,constantsConfigure.getFileHeader(),constantsConfigure.getFileLocation());
|
||
String fileSize = FileUpload.getFileSize(file.getInputStream().available());
|
||
System.out.println("file url is -> "+url);
|
||
String fileType = mFileTypes.get(suffix);
|
||
Sysdocument sysdocument = addDocument(OTCId,OTCType,url,fileType,fileName,userId,fileSize);
|
||
List<Sysdocument> sysdocuments = new ArrayList<>();
|
||
sysdocuments.add(sysdocument);
|
||
fileResult.setData(sysdocument);
|
||
success = true;
|
||
}
|
||
|
||
System.out.println("url -> "+url);
|
||
}catch (Exception e){
|
||
e.printStackTrace();
|
||
}
|
||
fileResult.setSuccess(success);
|
||
return fileResult;
|
||
}
|
||
|
||
|
||
/**
|
||
* 文件查询
|
||
* @return
|
||
* @throws Exception
|
||
*/
|
||
@ApiOperation(value = "文件查询", notes = "文件查询")
|
||
@ApiImplicitParams({
|
||
@ApiImplicitParam(name = "OTCId", value = "目标uuid 多个逗号隔开", required = true, dataType = "string"),
|
||
@ApiImplicitParam(name = "OTCType", value = "文件类型", required = true, dataType = "string")
|
||
})
|
||
@ResponseBody
|
||
@RequestMapping(value = "/getFile", method = RequestMethod.GET)
|
||
public FileResults getFile(String OTCId, String OTCType)throws Exception{
|
||
FileResults fileResults = new FileResults();
|
||
Boolean success = true;
|
||
String[] strs = OTCId.split(",");
|
||
if(strs.length > 0){
|
||
List<String> otcIds = Arrays.asList(strs);
|
||
List<Sysdocument> sysdocuments = sysdocumentMapper.findByOtcId(otcIds,OTCType);
|
||
if(sysdocuments.size() > 0){
|
||
fileResults.setData(sysdocuments);
|
||
success = true;
|
||
}else{
|
||
if("SysPhoto".equals(OTCType)){
|
||
List<Sysdocument> sysdocumentList = new ArrayList<>();
|
||
Sysdocument sysdocument = new Sysdocument();
|
||
sysdocument.setSysDocumentId(RandomNumber.getUUid());
|
||
sysdocumentList.add(sysdocument);
|
||
fileResults.setData(sysdocumentList);
|
||
}else{
|
||
fileResults.setData(new ArrayList<>());
|
||
}
|
||
}
|
||
}else{
|
||
fileResults.setData(new ArrayList<>());
|
||
}
|
||
fileResults.setSuccess(success);
|
||
return fileResults;
|
||
}
|
||
|
||
/**
|
||
* 删除文件
|
||
* @param deletedId
|
||
* @return
|
||
* @throws Exception
|
||
*/
|
||
@ApiOperation(value = "删除文件", notes = "删除文件")
|
||
@ApiImplicitParam(name = "deletedId",value = "文档表主键 sysDocumentId",required = true)
|
||
@ResponseBody
|
||
@RequestMapping(value = "/delFile", method = RequestMethod.GET)
|
||
public FileResult delFile(String deletedId)throws Exception{
|
||
FileResult fileResult = new FileResult();
|
||
sysdocumentMapper.delById(deletedId);
|
||
fileResult.setSuccess(true);
|
||
fileResult.setCode(Code.SUCCESS.getCode());
|
||
return fileResult;
|
||
}
|
||
|
||
/**
|
||
* 删除文件
|
||
* @param deletedId
|
||
* @return
|
||
* @throws Exception
|
||
*/
|
||
@ApiOperation(value = "删除文件", notes = "删除文件")
|
||
@ApiImplicitParam(name = "deletedId",value = "文档表主键 sysDocumentId",required = true)
|
||
@ResponseBody
|
||
@RequestMapping(value = "/deleteFile", method = RequestMethod.GET)
|
||
public FileResult deleteFile(String deletedId)throws Exception{
|
||
FileResult fileResult = new FileResult();
|
||
sysdocumentMapper.delById(deletedId);
|
||
fileResult.setSuccess(true);
|
||
fileResult.setCode(Code.SUCCESS.getCode());
|
||
return fileResult;
|
||
}
|
||
|
||
|
||
|
||
|
||
|
||
|
||
}
|