完善图片上传功能

This commit is contained in:
liuchengqian 2022-02-28 15:44:43 +08:00
parent 3d053e63de
commit 35fe6011e9
26 changed files with 307 additions and 832 deletions

View File

@ -4,7 +4,7 @@ import com.xkrs.dao.QcInspectionDataSourceDao;
import com.xkrs.encapsulation.PromptMessageEnum; import com.xkrs.encapsulation.PromptMessageEnum;
import com.xkrs.model.entity.QcInspectionDataSource; import com.xkrs.model.entity.QcInspectionDataSource;
import com.xkrs.model.qo.QcInspectionDataSourceQo; import com.xkrs.model.qo.QcInspectionDataSourceQo;
import com.xkrs.service.DataSourceService; import com.xkrs.service.QcInspectionDataSourceService;
import org.springframework.context.i18n.LocaleContextHolder; import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.PostMapping;
@ -22,10 +22,10 @@ import static com.xkrs.encapsulation.OutputEncapsulation.outputEncapsulationObje
* @Date: 2022/1/19 17:05 * @Date: 2022/1/19 17:05
*/ */
@RestController @RestController
public class DataSourceController { public class QcInspectionDataSourceController {
@Resource @Resource
private DataSourceService dataSourceService; private QcInspectionDataSourceService inspectionDataSourceService;
@Resource @Resource
private QcInspectionDataSourceDao inspectionDataSourceDao; private QcInspectionDataSourceDao inspectionDataSourceDao;
@ -38,7 +38,7 @@ public class DataSourceController {
*/ */
@PostMapping("/insertDataSource") @PostMapping("/insertDataSource")
public String insertDataSource(@RequestBody QcInspectionDataSourceQo inspectionDataSourceQo) { public String insertDataSource(@RequestBody QcInspectionDataSourceQo inspectionDataSourceQo) {
return dataSourceService.insertDataSource(inspectionDataSourceQo); return inspectionDataSourceService.insertDataSource(inspectionDataSourceQo);
} }
/** /**

View File

@ -3,7 +3,7 @@ package com.xkrs.controller;
import com.xkrs.dao.QcInspectionFileDao; import com.xkrs.dao.QcInspectionFileDao;
import com.xkrs.encapsulation.PromptMessageEnum; import com.xkrs.encapsulation.PromptMessageEnum;
import com.xkrs.model.qo.QcInspectionFileQo; import com.xkrs.model.qo.QcInspectionFileQo;
import com.xkrs.service.FileService; import com.xkrs.service.QcInspectionFileService;
import com.xkrs.util.ExcelUploadUtil; import com.xkrs.util.ExcelUploadUtil;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException; import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.springframework.context.i18n.LocaleContextHolder; import org.springframework.context.i18n.LocaleContextHolder;
@ -25,10 +25,10 @@ import static com.xkrs.encapsulation.OutputEncapsulation.outputEncapsulationObje
* @Date: 2022/1/20 16:01 * @Date: 2022/1/20 16:01
*/ */
@RestController @RestController
public class FileController { public class QcInspectionFileController {
@Resource @Resource
private FileService fileService; private QcInspectionFileService inspectionFileService;
@Resource @Resource
private QcInspectionFileDao inspectionFileDao; private QcInspectionFileDao inspectionFileDao;
@ -43,7 +43,7 @@ public class FileController {
*/ */
@PostMapping("/insertFileExcel") @PostMapping("/insertFileExcel")
public String insertFileExcel(QcInspectionFileQo inspectionFileQo, @RequestParam("fileExcel") MultipartFile fileExcel) throws IOException { public String insertFileExcel(QcInspectionFileQo inspectionFileQo, @RequestParam("fileExcel") MultipartFile fileExcel) throws IOException {
return fileService.insertFileExcel(inspectionFileQo, fileExcel); return inspectionFileService.insertFileExcel(inspectionFileQo, fileExcel);
} }
/** /**
@ -76,6 +76,6 @@ public class FileController {
*/ */
@PostMapping("/exportExcel") @PostMapping("/exportExcel")
public String exportExcel(@RequestBody QcInspectionFileQo inspectionFileQo) throws IOException, InvalidFormatException { public String exportExcel(@RequestBody QcInspectionFileQo inspectionFileQo) throws IOException, InvalidFormatException {
return fileService.exportExcel(inspectionFileQo); return inspectionFileService.exportExcel(inspectionFileQo);
} }
} }

View File

@ -4,7 +4,7 @@ import com.xkrs.dao.QcInspectionItemDictDao;
import com.xkrs.encapsulation.PromptMessageEnum; import com.xkrs.encapsulation.PromptMessageEnum;
import com.xkrs.model.entity.QcInspectionItemDict; import com.xkrs.model.entity.QcInspectionItemDict;
import com.xkrs.model.qo.QcInspectionItemDictQo; import com.xkrs.model.qo.QcInspectionItemDictQo;
import com.xkrs.service.DataDictService; import com.xkrs.service.QcInspectionItemDictService;
import com.xkrs.util.Query; import com.xkrs.util.Query;
import org.springframework.context.i18n.LocaleContextHolder; import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
@ -21,10 +21,10 @@ import static com.xkrs.encapsulation.OutputEncapsulation.outputEncapsulationObje
* @Date: 2022/1/19 10:54 * @Date: 2022/1/19 10:54
*/ */
@RestController @RestController
public class DataDictController { public class QcInspectionItemDictController {
@Resource @Resource
private DataDictService dataDictService; private QcInspectionItemDictService inspectionItemDictService;
@Resource @Resource
private QcInspectionItemDictDao inspectionItemDictDao; private QcInspectionItemDictDao inspectionItemDictDao;
@ -40,7 +40,7 @@ public class DataDictController {
*/ */
@PostMapping("/insertDataDict") @PostMapping("/insertDataDict")
public String insertDataDict(@RequestBody QcInspectionItemDictQo inspectionItemDictQo) { public String insertDataDict(@RequestBody QcInspectionItemDictQo inspectionItemDictQo) {
return dataDictService.insertDataDict(inspectionItemDictQo); return inspectionItemDictService.insertDataDict(inspectionItemDictQo);
} }
/** /**

View File

@ -0,0 +1,32 @@
package com.xkrs.controller;
import com.xkrs.model.qo.QcInspectionPictureQo;
import com.xkrs.service.QcInspectionPictureService;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import javax.annotation.Resource;
import java.io.IOException;
/**
* @Author: XinYi Song
* @Date: 2022/1/20 16:01
*/
@RestController
public class QcInspectionPictureController {
@Resource
private QcInspectionPictureService inspectionPictureService;
/**
* 上传图片
*/
@PostMapping("/insertPicture")
public String insertPicture(QcInspectionPictureQo inspectionPictureQo, @RequestParam("filePicture") MultipartFile pictureFile) throws IOException {
return inspectionPictureService.insertPicture(inspectionPictureQo, pictureFile);
}
}

View File

@ -0,0 +1,54 @@
package com.xkrs.controller;
import com.xkrs.dao.QcInspectionSpecDao;
import com.xkrs.encapsulation.PromptMessageEnum;
import com.xkrs.model.entity.QcInspectionSpec;
import com.xkrs.model.qo.QcInspectionSpecQo;
import com.xkrs.service.QcInspectionSpecService;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import javax.annotation.Resource;
import java.util.List;
import java.util.Locale;
import static com.xkrs.encapsulation.OutputEncapsulation.outputEncapsulationObject;
public class QcInspectionSpecController {
@Resource
private QcInspectionSpecService inspectionSpecService;
@Resource
private QcInspectionSpecDao inspectionSpecDao;
/**
* 添加规格数据
*
* @param inspectionSpecQo
* @return
*/
@PostMapping("/insertSpec")
public String insertSpec(@RequestBody QcInspectionSpecQo inspectionSpecQo) {
return inspectionSpecService.insertSpec(inspectionSpecQo);
}
/**
* 查询规格信息
*/
@GetMapping("/selectSpec")
public String selectSpec(@RequestBody QcInspectionSpecQo inspectionSpecQo) {
Locale locale = LocaleContextHolder.getLocale();
String partNo = inspectionSpecQo.getPartNo();
String inspectionItemCode = inspectionSpecQo.getInspectionItemCode();
List<QcInspectionSpec> inspectionSpecs = inspectionSpecDao.findByPartNoAndInspectionItemCode(partNo, inspectionItemCode);
if (inspectionSpecs == null || inspectionSpecs.isEmpty()) {
return outputEncapsulationObject(PromptMessageEnum.DATA_NONE, "暂时没有该零件的规格信息!", locale);
}
return outputEncapsulationObject(PromptMessageEnum.SUCCESS, inspectionSpecs.get(0), locale);
}
}

View File

@ -7,6 +7,8 @@ import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query; import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import java.util.List;
/** /**
* @Author: XinYi Song * @Author: XinYi Song
* @Date: 2022/1/20 15:23 * @Date: 2022/1/20 15:23
@ -24,7 +26,8 @@ public interface QcInspectionFileDao extends JpaRepository<QcInspectionFile, Lon
* @param partNo 零件号 * @param partNo 零件号
* @return * @return
*/ */
QcInspectionFile selectFile(String lotNo, String machineNo, String materialNo, String modelNo, String partNo); @Query(value = "from QcInspectionFile where lotNo = ?1 and machineNo = ?2 and materialNo = ?3 and modelNo = ?4 and partNo = ?5")
List<QcInspectionFile> selectFile(String lotNo, String machineNo, String materialNo, String modelNo, String partNo);
/** /**
* 修改模板上传信息 * 修改模板上传信息

View File

@ -1,6 +1,5 @@
package com.xkrs.dao; package com.xkrs.dao;
import com.xkrs.model.entity.QcInspectionFile;
import com.xkrs.model.entity.QcInspectionPicture; import com.xkrs.model.entity.QcInspectionPicture;
import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor; import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
@ -8,45 +7,36 @@ import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query; import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import java.util.List;
@Component @Component
public interface QcInspectionPictureDao extends JpaRepository<QcInspectionPicture, Long>, JpaSpecificationExecutor<QcInspectionPicture> { public interface QcInspectionPictureDao extends JpaRepository<QcInspectionPicture, Long>, JpaSpecificationExecutor<QcInspectionPicture> {
/** /**
* 根据机种号批次号机器号产品号查询文件信息 * 查询图片信息
* *
* @param fileModelNumber * @param lotNo 批次号
* @param fileBatchNumber * @param machineNo 机器号
* @param fileMachineCode * @param materialNo 物料号
* @param fileProductNumber * @param modelNo 模具号
* @param partNo 零件号
* @return * @return
*/ */
QcInspectionFile findByFileModelNumberAndFileBatchNumberAndFileMachineCodeAndFileProductNumber(String fileModelNumber, String fileBatchNumber, String fileMachineCode, String fileProductNumber); @Query(value = "from QcInspectionPicture where lotNo = ?1 and machineNo = ?2 and materialNo = ?3 and modelNo = ?4 and partNo = ?5")
List<QcInspectionPicture> selectPicture(String lotNo, String machineNo, String materialNo, String modelNo, String partNo);
/** /**
* 根据机种号批次号机器号产品号修改模板上传信息 * 修改图片路径信息
* *
* @param fileModelNumber * @param lotNo 批次号
* @param fileBatchNumber * @param machineNo 机器号
* @param fileMachineCode * @param materialNo 物料号
* @param fileProductNumber * @param modelNo 模具号
* @param fileUploadPath * @param partNo 零件号
* @param picturePath 图片路径
*/ */
@Modifying(clearAutomatically = true) @Modifying(clearAutomatically = true)
@Query(value = "update file set file_upload_path = ?5 where file_model_number = ?1 and file_batch_number = ?2 and file_machine_code = ?3 and file_product_number = ?4", nativeQuery = true) @Query(value = "update QcInspectionPicture set picturePath = ?6 where lotNo = ?1 and machineNo = ?2 and materialNo = ?3 and modelNo = ?4 and partNo = ?5")
void updateFileUploadPath(String fileModelNumber, String fileBatchNumber, String fileMachineCode, String fileProductNumber, String fileUploadPath); void updatePicturePath(String lotNo, String machineNo, String materialNo, String modelNo, String partNo, String picturePath);
/**
* 根据机种号批次号机器号产品号修改模板载下信息
*
* @param fileModelNumber
* @param fileBatchNumber
* @param fileMachineCode
* @param fileProductNumber
* @param fileDownloadPath
*/
@Modifying(clearAutomatically = true)
@Query(value = "update file set file_download_path = ?5 where file_model_number = ?1 and file_batch_number = ?2 and file_machine_code = ?3 and file_product_number = ?4", nativeQuery = true)
void updateFileDownloadPath(String fileModelNumber, String fileBatchNumber, String fileMachineCode, String fileProductNumber, String fileDownloadPath);
} }

View File

@ -1,10 +1,15 @@
package com.xkrs.dao; package com.xkrs.dao;
import com.xkrs.model.entity.QcInspectionItemDict;
import com.xkrs.model.entity.QcInspectionSpec; import com.xkrs.model.entity.QcInspectionSpec;
import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor; import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import java.util.List;
@Component @Component
public interface QcInspectionSpecDao extends JpaRepository<QcInspectionSpec, Long>, JpaSpecificationExecutor<QcInspectionSpec> { public interface QcInspectionSpecDao extends JpaRepository<QcInspectionSpec, Long>, JpaSpecificationExecutor<QcInspectionSpec> {
List<QcInspectionSpec> findByPartNoAndInspectionItemCode(String partNo,String inspectionItemCode);
} }

View File

@ -58,12 +58,6 @@ public class QcInspectionDataSource {
@Column(length = 85, columnDefinition = "varchar(85)") @Column(length = 85, columnDefinition = "varchar(85)")
private String inspectValue; private String inspectValue;
/**
* 质检规格编号
*/
@Column(length = 85, columnDefinition = "varchar(85)")
private String inspectionSpecNo;
public QcInspectionDataSource() { public QcInspectionDataSource() {
} }
@ -131,16 +125,8 @@ public class QcInspectionDataSource {
this.inspectValue = inspectValue; this.inspectValue = inspectValue;
} }
public String getInspectionSpecNo() {
return inspectionSpecNo;
}
public void setInspectionSpecNo(String inspectionSpecNo) {
this.inspectionSpecNo = inspectionSpecNo;
}
@Override @Override
public String toString() { public String toString() {
return "QcInspectionDataSource{" + "id=" + id + ", lotNo='" + lotNo + '\'' + ", machineNo='" + machineNo + '\'' + ", materialNo='" + materialNo + '\'' + ", modelNo='" + modelNo + '\'' + ", partNo='" + partNo + '\'' + ", inspectionItemCode='" + inspectionItemCode + '\'' + ", inspectValue='" + inspectValue + '\'' + ", inspectionSpecNo='" + inspectionSpecNo + '\'' + '}'; return "QcInspectionDataSource{" + "id=" + id + ", lotNo='" + lotNo + '\'' + ", machineNo='" + machineNo + '\'' + ", materialNo='" + materialNo + '\'' + ", modelNo='" + modelNo + '\'' + ", partNo='" + partNo + '\'' + ", inspectionItemCode='" + inspectionItemCode + '\'' + ", inspectValue='" + inspectValue + '\'' + '}';
} }
} }

View File

@ -42,11 +42,6 @@ public class QcInspectionDataSourceQo {
*/ */
private String inspectValue; private String inspectValue;
/**
* 质检规格编号
*/
private String inspectionSpecNo;
public QcInspectionDataSourceQo() { public QcInspectionDataSourceQo() {
} }
@ -114,11 +109,4 @@ public class QcInspectionDataSourceQo {
this.inspectValue = inspectValue; this.inspectValue = inspectValue;
} }
public String getInspectionSpecNo() {
return inspectionSpecNo;
}
public void setInspectionSpecNo(String inspectionSpecNo) {
this.inspectionSpecNo = inspectionSpecNo;
}
} }

View File

@ -1,67 +0,0 @@
package com.xkrs.model.vo;
import java.io.Serializable;
/**
* FileServerResultVo 文件服务器查询结果
* @author tajochen
*/
public class FileServerResultVo<T> implements Serializable {
/**
* 状态信息
*/
String status;
/**
* 提示信息
*/
String message;
/**
* 数据
*/
T data;
public FileServerResultVo() {
}
public FileServerResultVo(String status, String msg, T data) {
this.status = status;
this.message = msg;
this.data = data;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public T getData() {
return data;
}
public void setData(T data) {
this.data = data;
}
@Override
public String toString() {
return "FileServerResultVo{" +
"status='" + status + '\'' +
", message='" + message + '\'' +
", data=" + data +
'}';
}
}

View File

@ -1,170 +0,0 @@
package com.xkrs.model.vo;
import java.io.Serializable;
/**
* FileServiceResDetailVo
* @author tajochen
*/
public class FileServiceResDetailVo implements Serializable {
/**
* 域名
*/
String domain;
/**
* 信息摘要
*/
String md5;
/**
* 上传时间戳
*/
Long mtime;
/**
* 文件路径
*/
String path;
/**
* 返回信息代码
*/
int retcode;
/**
* 返回信息文本
*/
String retmsg;
/**
* 场景
*/
String scene;
/**
* 场景集合
*/
String scenes;
/**
* 文件大小
*/
long size;
/**
* 文件路径
*/
String src;
/**
* 文件完整链接
*/
String url;
public String getDomain() {
return domain;
}
public void setDomain(String domain) {
this.domain = domain;
}
public String getMd5() {
return md5;
}
public void setMd5(String md5) {
this.md5 = md5;
}
public Long getMtime() {
return mtime;
}
public void setMtime(Long mtime) {
this.mtime = mtime;
}
public String getPath() {
return path;
}
public void setPath(String path) {
this.path = path;
}
public int getRetcode() {
return retcode;
}
public void setRetcode(int retcode) {
this.retcode = retcode;
}
public String getRetmsg() {
return retmsg;
}
public void setRetmsg(String retmsg) {
this.retmsg = retmsg;
}
public String getScene() {
return scene;
}
public void setScene(String scene) {
this.scene = scene;
}
public String getScenes() {
return scenes;
}
public void setScenes(String scenes) {
this.scenes = scenes;
}
public long getSize() {
return size;
}
public void setSize(long size) {
this.size = size;
}
public String getSrc() {
return src;
}
public void setSrc(String src) {
this.src = src;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
@Override
public String toString() {
return "FileServiceResVo{" +
"domain='" + domain + '\'' +
", md5='" + md5 + '\'' +
", mtime=" + mtime +
", path='" + path + '\'' +
", retcode=" + retcode +
", retmsg='" + retmsg + '\'' +
", scene='" + scene + '\'' +
", scenes='" + scenes + '\'' +
", size=" + size +
", src='" + src + '\'' +
", url='" + url + '\'' +
'}';
}
}

View File

@ -1,64 +0,0 @@
package com.xkrs.service;
import com.xkrs.model.vo.FileServerResultVo;
import com.xkrs.model.vo.FileServiceResDetailVo;
import java.io.File;
/**
* 文件服务器接口
* @author tajochen
*/
public interface FileServerService {
/**
* 上传文件
* @param postFile 文件
* @param path 路径
* @return
*/
FileServiceResDetailVo uploadFile(File postFile, String path);
/**
* 获取文件统计信息
* @return
*/
FileServerResultVo getFileStat();
/**
* 删除文件
* @param md5 信息摘要
* @param path 路径
* @return
*/
FileServerResultVo deleteFile(String md5,String path);
/**
* 获取文件信息
* @param md5 信息摘要
* @param path 路径
* @return
*/
FileServerResultVo getFileInfo(String md5,String path);
/**
* 获取文件列表
* @param dir 目录名
* @return
*/
FileServerResultVo getFileList(String dir);
/**
* 修复统计信息
* @param date 要修复的日期格式如20190725
* @return
*/
FileServerResultVo getFileRepairStat(String date);
/**
* 同步失败修复
* @param force 是否强行修复(0|1)
* @return
*/
FileServerResultVo getFileRepair(String force);
}

View File

@ -6,7 +6,7 @@ import com.xkrs.model.qo.QcInspectionDataSourceQo;
* @Author: XinYi Song * @Author: XinYi Song
* @Date: 2022/1/19 16:42 * @Date: 2022/1/19 16:42
*/ */
public interface DataSourceService { public interface QcInspectionDataSourceService {
/** /**
* 添加测量数据 * 添加测量数据

View File

@ -10,7 +10,7 @@ import java.io.IOException;
* @Author: XinYi Song * @Author: XinYi Song
* @Date: 2022/1/20 15:24 * @Date: 2022/1/20 15:24
*/ */
public interface FileService { public interface QcInspectionFileService {
/** /**
* 添加模板信息 * 添加模板信息

View File

@ -6,7 +6,7 @@ import com.xkrs.model.qo.QcInspectionItemDictQo;
* @Author: XinYi Song * @Author: XinYi Song
* @Date: 2022/1/19 10:10 * @Date: 2022/1/19 10:10
*/ */
public interface DataDictService { public interface QcInspectionItemDictService {
/** /**
* 添加字典数据 * 添加字典数据

View File

@ -0,0 +1,18 @@
package com.xkrs.service;
import com.xkrs.model.qo.QcInspectionPictureQo;
import org.springframework.web.multipart.MultipartFile;
import java.io.IOException;
public interface QcInspectionPictureService {
/**
* 添加图片
*
* @param inspectionPictureQo
* @return
*/
String insertPicture(QcInspectionPictureQo inspectionPictureQo, MultipartFile pictureFile) throws IOException;
}

View File

@ -0,0 +1,15 @@
package com.xkrs.service;
import com.xkrs.model.qo.QcInspectionSpecQo;
public interface QcInspectionSpecService {
/**
* 添加规格数据
*
* @param inspectionSpecQo
* @return
*/
String insertSpec(QcInspectionSpecQo inspectionSpecQo);
}

View File

@ -1,150 +0,0 @@
package com.xkrs.service.impl;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.xkrs.model.vo.FileServerResultVo;
import com.xkrs.model.vo.FileServiceResDetailVo;
import com.xkrs.service.FileServerService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import java.io.File;
import java.util.HashMap;
import java.util.Map;
import static com.xkrs.util.FileServerTool.postFileManage;
import static com.xkrs.util.RequestUtil.postFile;
/**
* 文件服务器接口
* @author tajochen
*/
@Service
public class FileServerServiceImpl implements FileServerService {
Logger logger = LoggerFactory.getLogger(FileServerServiceImpl.class);
@Value("${my.FileServerAdminAdress}")
private String fileServerAdminAdress;
@Value("${my.FileServerAdress}")
private String fileServerAdress;
/**
* 上传文件
* @param theFile 本地文件
* @param path 文件服务器路径 项目名/功能模块名/具体分类名根据业务定
* @return
*/
@Override
public FileServiceResDetailVo uploadFile(File theFile, String path){
Map<String,String> postParam = new HashMap<>(16);
postParam.put("output","json");
postParam.put("path",path);
postParam.put("scene","");
String resFileServer = postFile(theFile, fileServerAdress+"/group1/upload", postParam);
ObjectMapper objectMapper = new ObjectMapper();
FileServiceResDetailVo result = null;
try {
result = objectMapper.readValue(resFileServer,FileServiceResDetailVo.class);
} catch (JsonProcessingException e) {
e.printStackTrace();
}
return result;
}
/**
* 获取文件统计信息
* @return
*/
@Override
public FileServerResultVo getFileStat(){
String requestUrl = fileServerAdminAdress + "/group1/stat";
Map<String,String> map = new HashMap<>(16);
return postFileManage(requestUrl,map);
}
/**
* 删除文件
* @param md5 信息摘要
* @param path 路径
* @return
*/
@Override
public FileServerResultVo deleteFile(String md5,String path){
String requestUrl = fileServerAdminAdress + "/group1/delete";
Map<String,String> map = new HashMap<>(16);
if(md5 != null && !md5.isEmpty()){
map.put("md5",md5);
}
if(path != null && !path.isEmpty()){
map.put("path",path);
}
return postFileManage(requestUrl,map);
}
/**
* 获取文件信息
* @param md5 信息摘要
* @param path 路径
* @return
*/
@Override
public FileServerResultVo getFileInfo(String md5,String path){
String requestUrl = fileServerAdminAdress + "/group1/get_file_info";
Map<String,String> map = new HashMap<>(16);
if(md5 != null && !md5.isEmpty()){
map.put("md5",md5);
}
if(path != null && !path.isEmpty()){
map.put("path",path);
}
return postFileManage(requestUrl,map);
}
/**
* 获取文件列表
* @param dir 目录名
* @return
*/
@Override
public FileServerResultVo getFileList(String dir){
String requestUrl = fileServerAdminAdress + "/group1/list_dir";
Map<String,String> map = new HashMap<>(16);
map.put("dir",dir);
return postFileManage(requestUrl,map);
}
/**
* 修复统计信息
* @param date 要修复的日期格式如20190725
* @return
*/
@Override
public FileServerResultVo getFileRepairStat(String date){
String requestUrl = fileServerAdminAdress + "/group1/repair_stat";
Map<String,String> map = new HashMap<>(16);
map.put("date",date);
return postFileManage(requestUrl,map);
}
/**
* 同步失败修复
* @param force 是否强行修复(0|1)
* @return
*/
@Override
public FileServerResultVo getFileRepair(String force){
String requestUrl = fileServerAdminAdress + "/group1/repair";
Map<String,String> map = new HashMap<>(16);
map.put("force",force);
return postFileManage(requestUrl,map);
}
public static void main(){
}
}

View File

@ -6,7 +6,7 @@ import com.xkrs.encapsulation.PromptMessageEnum;
import com.xkrs.model.entity.QcInspectionDataSource; import com.xkrs.model.entity.QcInspectionDataSource;
import com.xkrs.model.entity.QcInspectionItemDict; import com.xkrs.model.entity.QcInspectionItemDict;
import com.xkrs.model.qo.QcInspectionDataSourceQo; import com.xkrs.model.qo.QcInspectionDataSourceQo;
import com.xkrs.service.DataSourceService; import com.xkrs.service.QcInspectionDataSourceService;
import org.springframework.context.i18n.LocaleContextHolder; import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
@ -20,7 +20,7 @@ import static com.xkrs.encapsulation.OutputEncapsulation.outputEncapsulationObje
* @Date: 2022/1/19 16:43 * @Date: 2022/1/19 16:43
*/ */
@Service @Service
public class DataSourceServiceImpl implements DataSourceService { public class QcInspectionDataSourceServiceImpl implements QcInspectionDataSourceService {
@Resource @Resource
private QcInspectionDataSourceDao inspectionDataSourceDao; private QcInspectionDataSourceDao inspectionDataSourceDao;
@ -37,12 +37,28 @@ public class DataSourceServiceImpl implements DataSourceService {
@Override @Override
public String insertDataSource(QcInspectionDataSourceQo inspectionDataSourceQo) { public String insertDataSource(QcInspectionDataSourceQo inspectionDataSourceQo) {
Locale locale = LocaleContextHolder.getLocale(); Locale locale = LocaleContextHolder.getLocale();
QcInspectionItemDict byDictEnglishName = inspectionItemDictDao.findByInspectionItemCode(inspectionDataSourceQo.getInspectionItemCode()); String lotNo = inspectionDataSourceQo.getLotNo();
if (byDictEnglishName == null) { String machineNo = inspectionDataSourceQo.getMachineNo();
String materialNo = inspectionDataSourceQo.getMaterialNo();
String modelNo = inspectionDataSourceQo.getModelNo();
String partNo = inspectionDataSourceQo.getPartNo();
String inspectionItemCode = inspectionDataSourceQo.getInspectionItemCode();
String inspectValue = inspectionDataSourceQo.getInspectValue();
QcInspectionItemDict byInspectionItemCode = inspectionItemDictDao.findByInspectionItemCode(inspectionDataSourceQo.getInspectionItemCode());
if (byInspectionItemCode == null) {
return outputEncapsulationObject(PromptMessageEnum.DATA_NONE, "不存在该字典类型!", locale); return outputEncapsulationObject(PromptMessageEnum.DATA_NONE, "不存在该字典类型!", locale);
} }
QcInspectionDataSource qcInspectionDataSource = new QcInspectionDataSource(); QcInspectionDataSource qcInspectionDataSource = new QcInspectionDataSource();
//TODO 补充字段 if (inspectionDataSourceQo.getId() > 0) {
qcInspectionDataSource.setId(inspectionDataSourceQo.getId());
}
qcInspectionDataSource.setLotNo(lotNo);
qcInspectionDataSource.setMachineNo(machineNo);
qcInspectionDataSource.setMaterialNo(materialNo);
qcInspectionDataSource.setModelNo(modelNo);
qcInspectionDataSource.setPartNo(partNo);
qcInspectionDataSource.setInspectionItemCode(inspectionItemCode);
qcInspectionDataSource.setInspectValue(inspectValue);
inspectionDataSourceDao.save(qcInspectionDataSource); inspectionDataSourceDao.save(qcInspectionDataSource);
return outputEncapsulationObject(PromptMessageEnum.SUCCESS, "添加成功!", locale); return outputEncapsulationObject(PromptMessageEnum.SUCCESS, "添加成功!", locale);
} }

View File

@ -7,7 +7,7 @@ import com.xkrs.model.entity.QcInspectionDataSource;
import com.xkrs.model.entity.QcInspectionFile; import com.xkrs.model.entity.QcInspectionFile;
import com.xkrs.model.entity.QcInspectionItemDict; import com.xkrs.model.entity.QcInspectionItemDict;
import com.xkrs.model.qo.QcInspectionFileQo; import com.xkrs.model.qo.QcInspectionFileQo;
import com.xkrs.service.FileService; import com.xkrs.service.QcInspectionFileService;
import com.xkrs.util.ExcelUploadUtil; import com.xkrs.util.ExcelUploadUtil;
import com.xkrs.util.ExportExcel; import com.xkrs.util.ExportExcel;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException; import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
@ -30,7 +30,7 @@ import static com.xkrs.encapsulation.OutputEncapsulation.outputEncapsulationObje
* @Date: 2022/1/20 15:24 * @Date: 2022/1/20 15:24
*/ */
@Service @Service
public class FileServiceImpl implements FileService { public class QcInspectionFileServiceImpl implements QcInspectionFileService {
@Resource @Resource
private QcInspectionFileDao inspectionFileDao; private QcInspectionFileDao inspectionFileDao;
@ -55,23 +55,25 @@ public class FileServiceImpl implements FileService {
String modelNo = inspectionFileQo.getModelNo(); String modelNo = inspectionFileQo.getModelNo();
String partNo = inspectionFileQo.getPartNo(); String partNo = inspectionFileQo.getPartNo();
Locale locale = LocaleContextHolder.getLocale(); Locale locale = LocaleContextHolder.getLocale();
QcInspectionFile qcInspectionFile = inspectionFileDao.selectFile(lotNo, machineNo, materialNo, modelNo, partNo); List<QcInspectionFile> qcInspectionFiles = inspectionFileDao.selectFile(lotNo, machineNo, materialNo, modelNo, partNo);
if (qcInspectionFile != null) { if (qcInspectionFiles != null || qcInspectionFiles.isEmpty()) {
return outputEncapsulationObject(PromptMessageEnum.DATA_EXIT, "已存在该产品的模板!", locale); return outputEncapsulationObject(PromptMessageEnum.DATA_EXIT, "已存在该产品的模板!", locale);
} }
if (fileExcel == null) { if (fileExcel == null) {
return outputEncapsulationObject(PromptMessageEnum.DATA_NONE, "模板不能为空!", locale); return outputEncapsulationObject(PromptMessageEnum.DATA_NONE, "模板不能为空!", locale);
} }
String file = ExcelUploadUtil.memoryFile(fileExcel); String file = ExcelUploadUtil.memoryFile(fileExcel);
QcInspectionFile fileEntity1 = new QcInspectionFile(); QcInspectionFile fileEntity = new QcInspectionFile();
//TODO 补充字段 if (inspectionFileQo.getId() > 0) {
// fileEntity1.setFileModelNumber(fileQo.getFileModelNumber()); fileEntity.setId(inspectionFileQo.getId());
// fileEntity1.setFileBatchNumber(fileQo.getFileBatchNumber()); }
// fileEntity1.setFileMachineCode(fileQo.getFileMachineCode()); fileEntity.setLotNo(lotNo);
// fileEntity1.setFileProductNumber(fileQo.getFileProductNumber()); fileEntity.setMachineNo(machineNo);
// fileEntity1.setFileUploadPath(file); fileEntity.setMaterialNo(materialNo);
fileEntity.setModelNo(modelNo);
inspectionFileDao.save(fileEntity1); fileEntity.setPartNo(partNo);
fileEntity.setTemplatePath(file);
inspectionFileDao.save(fileEntity);
return outputEncapsulationObject(PromptMessageEnum.SUCCESS, "添加成功!", locale); return outputEncapsulationObject(PromptMessageEnum.SUCCESS, "添加成功!", locale);
} }
@ -90,11 +92,11 @@ public class FileServiceImpl implements FileService {
String modelNo = inspectionFileQo.getModelNo(); String modelNo = inspectionFileQo.getModelNo();
String partNo = inspectionFileQo.getPartNo(); String partNo = inspectionFileQo.getPartNo();
Locale locale = LocaleContextHolder.getLocale(); Locale locale = LocaleContextHolder.getLocale();
QcInspectionFile inspectionFile = inspectionFileDao.selectFile(lotNo, machineNo, materialNo, modelNo, partNo); List<QcInspectionFile> qcInspectionFiles = inspectionFileDao.selectFile(lotNo, machineNo, materialNo, modelNo, partNo);
if (inspectionFile == null) { if (qcInspectionFiles == null || qcInspectionFiles.isEmpty()) {
return outputEncapsulationObject(PromptMessageEnum.DATA_NONE, "暂时没有该产品的模板,请添加!", locale); return outputEncapsulationObject(PromptMessageEnum.DATA_NONE, "暂时没有该产品的模板,请添加!", locale);
} }
String templatePath = inspectionFile.getTemplatePath(); String templatePath = qcInspectionFiles.get(0).getTemplatePath();
List<QcInspectionDataSource> inspectionDataSources = inspectionDataSourceDao.selectDataSource(lotNo, machineNo, materialNo, modelNo, partNo); List<QcInspectionDataSource> inspectionDataSources = inspectionDataSourceDao.selectDataSource(lotNo, machineNo, materialNo, modelNo, partNo);
if (inspectionDataSources == null || inspectionDataSources.isEmpty()) { if (inspectionDataSources == null || inspectionDataSources.isEmpty()) {
return outputEncapsulationObject(PromptMessageEnum.DATA_NONE, "暂时没有该产品的信息!", locale); return outputEncapsulationObject(PromptMessageEnum.DATA_NONE, "暂时没有该产品的信息!", locale);

View File

@ -4,7 +4,7 @@ import com.xkrs.dao.QcInspectionItemDictDao;
import com.xkrs.encapsulation.PromptMessageEnum; import com.xkrs.encapsulation.PromptMessageEnum;
import com.xkrs.model.entity.QcInspectionItemDict; import com.xkrs.model.entity.QcInspectionItemDict;
import com.xkrs.model.qo.QcInspectionItemDictQo; import com.xkrs.model.qo.QcInspectionItemDictQo;
import com.xkrs.service.DataDictService; import com.xkrs.service.QcInspectionItemDictService;
import org.springframework.context.i18n.LocaleContextHolder; import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
@ -18,7 +18,7 @@ import static com.xkrs.encapsulation.OutputEncapsulation.outputEncapsulationObje
* @Date: 2022/1/19 10:28 * @Date: 2022/1/19 10:28
*/ */
@Service @Service
public class DataDictServiceImpl implements DataDictService { public class QcInspectionItemDictServiceImpl implements QcInspectionItemDictService {
@Resource @Resource
private QcInspectionItemDictDao inspectionItemDictDao; private QcInspectionItemDictDao inspectionItemDictDao;
@ -32,12 +32,6 @@ public class DataDictServiceImpl implements DataDictService {
@Override @Override
public String insertDataDict(QcInspectionItemDictQo inspectionItemDictQo) { public String insertDataDict(QcInspectionItemDictQo inspectionItemDictQo) {
Locale locale = LocaleContextHolder.getLocale(); Locale locale = LocaleContextHolder.getLocale();
//DataDict byDictChineseName = inspectionItemDictDao.findByDictChineseName(dataDictQo.getDictChineseName());
/*DataDict chineseName = inspectionItemDictDao.findByModelNumberAndBatchNumberAndMachineCodeAndDictProductNumberAndDictChineseName(dataDictQo.getModelNumber(), dataDictQo.getBatchNumber(), dataDictQo.getMachineCode(), dataDictQo.getDictProductNumber(), dataDictQo.getDictChineseName());
if(chineseName != null){
return outputEncapsulationObject(PromptMessageEnum.DATA_EXIT,"该产品变量已存在,请勿重复添加!",locale);
}*/
//DataDict byDictEnglishName = inspectionItemDictDao.findByDictEnglishName(dataDictQo.getDictEnglishName());
QcInspectionItemDict englishName = inspectionItemDictDao.findByInspectionItemCode(inspectionItemDictQo.getInspectionItemCode()); QcInspectionItemDict englishName = inspectionItemDictDao.findByInspectionItemCode(inspectionItemDictQo.getInspectionItemCode());
if (englishName != null) { if (englishName != null) {
return outputEncapsulationObject(PromptMessageEnum.DATA_EXIT, "该产品变量已存在,请勿重复添加!", locale); return outputEncapsulationObject(PromptMessageEnum.DATA_EXIT, "该产品变量已存在,请勿重复添加!", locale);

View File

@ -0,0 +1,38 @@
package com.xkrs.service.impl;
import com.xkrs.dao.QcInspectionPictureDao;
import com.xkrs.encapsulation.PromptMessageEnum;
import com.xkrs.model.entity.QcInspectionPicture;
import com.xkrs.model.qo.QcInspectionPictureQo;
import com.xkrs.service.QcInspectionPictureService;
import com.xkrs.util.ExcelUploadUtil;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.web.multipart.MultipartFile;
import javax.annotation.Resource;
import java.io.IOException;
import java.util.Locale;
import static com.xkrs.encapsulation.OutputEncapsulation.outputEncapsulationObject;
public class QcInspectionPictureServiceImpl implements QcInspectionPictureService {
@Resource
private QcInspectionPictureDao inspectionPictureDao;
@Override
public String insertPicture(QcInspectionPictureQo inspectionPictureQo, MultipartFile pictureFile) throws IOException {
Locale locale = LocaleContextHolder.getLocale();
String file = ExcelUploadUtil.memoryFile(pictureFile);
QcInspectionPicture qcInspectionPicture = new QcInspectionPicture();
qcInspectionPicture.setId(inspectionPictureQo.getId());
qcInspectionPicture.setLotNo(inspectionPictureQo.getLotNo());
qcInspectionPicture.setMachineNo(inspectionPictureQo.getMachineNo());
qcInspectionPicture.setMaterialNo(inspectionPictureQo.getMaterialNo());
qcInspectionPicture.setModelNo(inspectionPictureQo.getModelNo());
qcInspectionPicture.setPartNo(inspectionPictureQo.getPartNo());
qcInspectionPicture.setPicturePath(file);
inspectionPictureDao.save(qcInspectionPicture);
return outputEncapsulationObject(PromptMessageEnum.SUCCESS, "添加成功!", locale);
}
}

View File

@ -0,0 +1,64 @@
package com.xkrs.service.impl;
import com.xkrs.dao.QcInspectionItemDictDao;
import com.xkrs.dao.QcInspectionSpecDao;
import com.xkrs.encapsulation.PromptMessageEnum;
import com.xkrs.model.entity.QcInspectionItemDict;
import com.xkrs.model.entity.QcInspectionSpec;
import com.xkrs.model.qo.QcInspectionSpecQo;
import com.xkrs.service.QcInspectionSpecService;
import org.springframework.context.i18n.LocaleContextHolder;
import javax.annotation.Resource;
import java.util.Locale;
import static com.xkrs.encapsulation.OutputEncapsulation.outputEncapsulationObject;
public class QcInspectionSpecServiceImpl implements QcInspectionSpecService {
@Resource
private QcInspectionSpecDao inspectionSpecDao;
@Resource
private QcInspectionItemDictDao inspectionItemDictDao;
/**
* 新增规格数据
*
* @param inspectionSpecQo
* @return
*/
@Override
public String insertSpec(QcInspectionSpecQo inspectionSpecQo) {
String partNo = inspectionSpecQo.getPartNo();
String inspectionItemCode = inspectionSpecQo.getInspectionItemCode();
String max = inspectionSpecQo.getMax();
String mean = inspectionSpecQo.getMean();
String min = inspectionSpecQo.getMin();
String unit = inspectionSpecQo.getUnit();
String inspectionMethod = inspectionSpecQo.getInspectionMethod();
String inspectionStandard = inspectionSpecQo.getInspectionStandard();
String remark = inspectionSpecQo.getRemark();
Locale locale = LocaleContextHolder.getLocale();
QcInspectionItemDict byInspectionItemCode = inspectionItemDictDao.findByInspectionItemCode(inspectionSpecQo.getInspectionItemCode());
if (byInspectionItemCode == null) {
return outputEncapsulationObject(PromptMessageEnum.DATA_NONE, "不存在该字典类型!", locale);
}
QcInspectionSpec inspectionSpec = new QcInspectionSpec();
if (inspectionSpecQo.getId() > 0) {
inspectionSpec.setId(inspectionSpecQo.getId());
}
inspectionSpec.setPartNo(partNo);
inspectionSpec.setInspectionItemCode(inspectionItemCode);
inspectionSpec.setMax(max);
inspectionSpec.setMean(mean);
inspectionSpec.setMin(min);
inspectionSpec.setUnit(unit);
inspectionSpec.setInspectionMethod(inspectionMethod);
inspectionSpec.setInspectionStandard(inspectionStandard);
inspectionSpec.setRemark(remark);
inspectionSpecDao.save(inspectionSpec);
return outputEncapsulationObject(PromptMessageEnum.SUCCESS, "添加成功!", locale);
}
}

View File

@ -1,91 +0,0 @@
package com.xkrs.util;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.xkrs.model.vo.FileServerResultVo;
import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
/**
* 文件服务器工具
* @author tajochen
*/
public class FileServerTool {
private static final String FIREFOX_UA = "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:91.0) " +
"Gecko/20100101 Firefox/91.0";
/**
* 发送管理命令到文件服务器
* @param url 文件服务器路径
* @param map 命令参数集
* @return
*/
public static FileServerResultVo postFileManage(String url, Map<String,String> map){
Logger log = LoggerFactory.getLogger(RequestUtil.class);
String body = "";
// 创建httpclient对象
CloseableHttpClient client = HttpClients.createDefault();
// 创建post方式请求对象
HttpPost httpPost = new HttpPost(url);
// 装填参数
List<NameValuePair> nvps = new ArrayList<>();
if(map!=null){
for (Map.Entry<String, String> entry : map.entrySet()) {
nvps.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
}
}
try {
// 设置参数到请求对象中
httpPost.setEntity(new UrlEncodedFormEntity(nvps, "UTF-8"));
// 设置header报文头信息
httpPost.setHeader("Content-type", "application/x-www-form-urlencoded");
httpPost.setHeader("User-Agent", FIREFOX_UA);
httpPost.setHeader("Accept", "application");
httpPost.setHeader("Accept-Encoding", "gzip, deflate");
// 执行请求操作并拿到结果同步阻塞
CloseableHttpResponse response = client.execute(httpPost);
// 获取结果实体
HttpEntity entity = response.getEntity();
if (entity != null) {
// 按指定编码转换结果实体为String类型
body = EntityUtils.toString(entity, "UTF-8");
}
EntityUtils.consume(entity);
// 释放链接
response.close();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
client.close();
} catch (IOException e) {
e.printStackTrace();
}
}
FileServerResultVo fileServerResultVo = new FileServerResultVo();
ObjectMapper mapper = new ObjectMapper();
try {
fileServerResultVo = mapper.readValue(body, FileServerResultVo.class);
} catch (JsonProcessingException e) {
e.printStackTrace();
log.warn(e.toString());
}
return fileServerResultVo;
}
}

View File

@ -1,188 +0,0 @@
package com.xkrs.util;
import org.apache.http.Consts;
import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.mime.HttpMultipartMode;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.entity.mime.content.StringBody;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.*;
/**
* http请求工具
* @author tajochen
**/
public class RequestUtil {
private static final String FIREFOX_UA = "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:92.0) Gecko/20100101 Firefox/92.0";
private static final String CHROME_UA = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 " +
"(KHTML, like Gecko) Chrome/93.0.4577.63 Safari/537.36";
/**
* 模拟 get请求
* @param url 链接
* @param map 参数列表
*/
public static String getStandard(String url, Map<String,String> map) {
String body = "";
// 1.拿到一个httpclient的对象
CloseableHttpClient httpClient = HttpClients.createDefault();
// 2.1 提交请求体
ArrayList<NameValuePair> parameters = new ArrayList<>();
URIBuilder builder = null;
// 装填参数
if(map!=null){
for (Map.Entry<String, String> entry : map.entrySet()) {
parameters.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
}
}
CloseableHttpResponse response = null;
try {
builder = new URIBuilder(url);
builder.setParameters(parameters);
// 2.设置请求方式和请求信息
HttpGet httpGet = new HttpGet(builder.build());
// 2.1 提交header头信息
httpGet.addHeader("user-agent", FIREFOX_UA);
// 3.执行请求
response = httpClient.execute(httpGet);
// 4.获取返回值
assert response != null;
// 按指定编码转换结果实体为String类型
body = EntityUtils.toString(response.getEntity(), StandardCharsets.UTF_8);
// 5.关闭连接
response.close();
httpClient.close();
} catch (Exception e) {
e.printStackTrace();
}
return body;
}
/**
* 模拟Post请求 application/x-www-form-urlencoded
* @param url 资源地址
* @param map 参数列表
* @return
*/
public static String postStandard(String url, Map<String,String> map) {
String body = "";
// 创建httpclient对象
CloseableHttpClient client = HttpClients.createDefault();
// 创建post方式请求对象
HttpPost httpPost = new HttpPost(url);
// 装填参数
List<NameValuePair> nvps = new ArrayList<>();
if(map!=null){
for (Map.Entry<String, String> entry : map.entrySet()) {
nvps.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
}
}
try {
// 设置参数到请求对象中
httpPost.setEntity(new UrlEncodedFormEntity(nvps, "UTF-8"));
// 设置header报文头信息
httpPost.setHeader("Content-type", "application/x-www-form-urlencoded");
httpPost.setHeader("User-Agent", FIREFOX_UA);
httpPost.setHeader("Accept", "application");
httpPost.setHeader("Accept-Encoding", "gzip, deflate");
// 执行请求操作并拿到结果同步阻塞
CloseableHttpResponse response = client.execute(httpPost);
// 获取结果实体
HttpEntity entity = response.getEntity();
if (entity != null) {
// 按指定编码转换结果实体为String类型
body = EntityUtils.toString(entity, "UTF-8");
}
EntityUtils.consume(entity);
// 释放链接
response.close();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
client.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return body;
}
/**
* 模拟Post请求 multipart/form-data
* @param postFile 请求文件
* @param postUrl 请求链接
* @param postParam 请求参数
* @return
*/
public static String postFile(File postFile, String postUrl, Map<String,String> postParam) {
Logger log = LoggerFactory.getLogger(RequestUtil.class);
String body = "";
Map<String,Object> resultMap = new HashMap<>(16);
CloseableHttpClient httpClient = HttpClients.createDefault();
try{
// 把一个普通参数和文件上传给下面这个地址
HttpPost httpPost = new HttpPost(postUrl);
// 设置传输参数
MultipartEntityBuilder multipartEntity = MultipartEntityBuilder.create();
// 解决乱码问题
multipartEntity.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
multipartEntity.setCharset(StandardCharsets.UTF_8);
// 设置文件参数 获取文件名postFile.getName()
// 把文件转换成流对象FileBody
multipartEntity.addBinaryBody("file",postFile,ContentType.create("multipart/form-data",
Consts.UTF_8),postFile.getName());
// 设计文件以外的参数
Set<String> keySet = postParam.keySet();
for (String key : keySet) {
// 相当于<input type="text" name="name" value=name>
multipartEntity.addPart(key, new StringBody(postParam.get(key), ContentType.create(
"multipart/form-data", Consts.UTF_8)));
}
HttpEntity reqEntity = multipartEntity.build();
httpPost.setEntity(reqEntity);
// 设置header报文头信息
httpPost.setHeader("User-Agent", FIREFOX_UA);
httpPost.setHeader("Accept","text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
httpPost.setHeader("Accept-Encoding","gzip, deflate");
// 发起请求返回请求的响应
try (CloseableHttpResponse response = httpClient.execute(httpPost)) {
resultMap.put("statusCode", response.getStatusLine().getStatusCode());
// 获取响应对象
HttpEntity resEntity = response.getEntity();
if (resEntity != null) {
// 按指定编码转换结果实体为String类型
body = EntityUtils.toString(resEntity, StandardCharsets.UTF_8);
}
// 销毁
EntityUtils.consume(resEntity);
} catch (Exception e) {
e.printStackTrace();
}
httpClient.close();
} catch (IOException e1) {
e1.printStackTrace();
}
return body;
}
}