上传OSS服务器配合工具类代码

package com.example.demo;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.mime.MultipartEntity;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.entity.mime.content.StringBody;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.BufferedReader;
import java.io.File;
import java.io.InputStreamReader;
import java.nio.charset.Charset;
import java.util.Map;


@CrossOrigin
@RestController
@RequestMapping(value = "/test")
public class newPolicyController {
private static Logger logger = LoggerFactory.getLogger(newPolicyController.class);

@Value("${api_key}")
private String api_key;

@Value("${api_secret}")
private String api_secret;

@Value("${idcard_Url}")
private String idcard_Url;

@Value("${identity_Url}")
private String identity_Url;

// 回调地址
private String uploadUrl = "";

@Autowired
private Util util;

/**
* 上传身份证
* 功能描述:
* @Author xiyou
* @Description
* @Date 18:03 2019/4/12
* @Param file 身份证照片
* @return
**/
@RequestMapping(value = "/UploadIdPicture",method = RequestMethod.POST)
public String UploadIdPicture(MultipartFile file) throws Exception {
if (null == file) {
return "上传文件不存在";
}
Map fileReName = util.fileReName(file);
String proFileName = (String) fileReName.get("proFileName");
String suffixname = (String) fileReName.get("suffixname");
// 压缩图片
util.zipPic(file, proFileName,suffixname);
File files = new File(proFileName + "." + suffixname);
HttpClient httpclient = new DefaultHttpClient();
HttpPost post = new HttpPost(idcard_Url);
FileBody fileBody = new FileBody(files);
MultipartEntity entity = new MultipartEntity();
entity.addPart("image_file", fileBody);
post.setEntity(entity);
String headerParam = GenerateString.genHeaderParam(api_key, api_secret);
post.setHeader("Authorization", headerParam);
HttpResponse response = httpclient.execute(post);
if (response.getStatusLine().getStatusCode() == 200) {
HttpEntity entitys = response.getEntity();
BufferedReader reader = new BufferedReader(
new InputStreamReader(entitys.getContent()));
String line = reader.readLine();
boolean idcardResultInfos = util.idcardResultInfos(line);
if(idcardResultInfos){
uploadUrl = util.upLoad(files);
if(uploadUrl != null){
logger.info("身份证信息正确,阿里云OSS上传成功---"+uploadUrl);
util.delFile(proFileName + "." + suffixname);
}
}else {
util.delFile(proFileName + "." + suffixname);
return "上传身份证不正确,请重新上传";
}
return line;
}else{
HttpEntity r_entity = response.getEntity();
String responseString = EntityUtils.toString(r_entity);
logger.error("错误码是:"+response.getStatusLine().getStatusCode()+" "+response.getStatusLine().getReasonPhrase());
logger.error("出错原因是:"+responseString);
}
httpclient.getConnectionManager().shutdown();
util.delFile(proFileName + "." + suffixname);
return "错误";
}

/**
* 上传活体视频
* 功能描述:
* @Author xiyou
* @Description
* @Date 18:05 2019/4/12
* @Param file 活体视频
* @Param username 姓名
* @Param id_number 身份证号
* @return
**/
@RequestMapping(value = "/UploadLiveVerification",method = RequestMethod.POST)
public CommonResult UploadLiveVerification(MultipartFile file,String username,String id_number) throws Exception {
CommonResult result = new CommonResult();
long startTime = System.currentTimeMillis(); //获取开始时间
if(file != null){
result.setResultCode("40");
result.setResultMsg("活体视频不存在");
}
String responseString = null;
Map fileReName = util.fileReName(file);
String filename = (String) fileReName.get("filename");
String proFileName = (String) fileReName.get("proFileName");
String suffixname = (String) fileReName.get("suffixname");
util.convetors(file,proFileName,suffixname);

HttpClient httpclient = new DefaultHttpClient();
HttpPost post = new HttpPost(identity_Url);
StringBody name = new StringBody(username, Charset.forName("UTF-8"));
StringBody number = new StringBody(id_number);
FileBody fileBody = new FileBody(new File(proFileName+"."+suffixname));
MultipartEntity entity = new MultipartEntity();

entity.addPart("name", name);
entity.addPart("idnumber", number);
entity.addPart("video_file", fileBody);
post.setEntity(entity);
String headerParam = GenerateString.genHeaderParam(api_key, api_secret);
post.setHeader("Authorization", headerParam);//请将AUTHORIZATION替换为根据API_KEY和API_SECRET得到的签名认证串
HttpResponse response = httpclient.execute(post);
if (response.getStatusLine().getStatusCode() == 200) {
HttpEntity entitys = response.getEntity();
BufferedReader reader = new BufferedReader(
new InputStreamReader(entitys.getContent()));
String line = reader.readLine();
System.out.println(line);
result = util.identityResultInfo(line);
boolean ocrResultInfos = util.ocrResultInfos(line);
if(ocrResultInfos){
uploadUrl = util.upLoad(new File(proFileName+"."+suffixname));
if(uploadUrl != null){
logger.info("活体认证通过,阿里云OSS上传成功---"+uploadUrl);
}
}
util.delFile(filename);
util.delFile(proFileName + "." + suffixname);
long endTime = System.currentTimeMillis(); //获取结束时间
System.out.println("程序运行时间:" + (endTime - startTime) + "ms"); //输出程序运行时间
return result;
}else{
HttpEntity r_entity = response.getEntity();
responseString= EntityUtils.toString(r_entity);
//你需要根据出错的原因判断错误信息,并修改
}
httpclient.getConnectionManager().shutdown();
result.setResultCode(response.getStatusLine().getStatusCode()+" "+response.getStatusLine().getReasonPhrase());
result.setResultMsg(responseString);
long endTime = System.currentTimeMillis(); //获取结束时间
System.out.println("程序运行时间:" + (endTime - startTime) + "ms"); //输出程序运行时间
return result;
}
}
posted @ 2019-04-26 18:46  帅总吖  阅读(114)  评论(0编辑  收藏  举报