You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

272 lines
12 KiB
Java

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

package com.api.sfj.DA.util;
import com.alibaba.fastjson.JSONObject;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import weaver.general.BaseBean;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
public class DaHttpAction {
private static String FILENAME = "";
private static String SESSION = "";
private static File file = new File("");
public static String getSESSION() {
return SESSION;
}
public static void setSESSION(String SESSION) {
DaHttpAction.SESSION = SESSION;
}
public String getFILENAME() {
return FILENAME;
}
public void setFILENAME(String FILENAME) {
this.FILENAME = FILENAME;
}
public File getFile() {
return file;
}
public void setFile(File file) {
this.file = file;
}
private static CloseableHttpClient httpClient = HttpClients.createDefault();
private static final String ADDRESS_URL = "http://10.6.115.68:8082";
private static String init() throws IOException {
BaseBean bs = new BaseBean();
bs.writeLog("进入归档初始化方法-->文件名称-->"+FILENAME);
bs.writeLog("进入归档初始化方法-->文件名称长度-->"+String.valueOf(file.length()));
String initpath = ADDRESS_URL + "/oa/InitUpload.do";
bs.writeLog("进入归档初始化方法-->上传接口路径-->"+initpath);
HttpPost httppost = new HttpPost(initpath);
MultipartEntityBuilder mEntityBuilder = MultipartEntityBuilder.create();
mEntityBuilder.addTextBody("fileName", FILENAME, ContentType.TEXT_PLAIN.withCharset("utf-8"));
mEntityBuilder.addTextBody("fileSize", String.valueOf(file.length()));
String reStr;
JSONObject jsonObject;
HttpResponse responce;
httppost.setEntity(mEntityBuilder.build());
try {
bs.writeLog("进入初始化try/catch-->"+httppost);
responce = httpClient.execute(httppost);
bs.writeLog("进入归档初始化方法--执行接口返回结果-->"+responce);
int resStatu = responce.getStatusLine().getStatusCode();
bs.writeLog("第一个接口--resStatu--->"+resStatu);
bs.writeLog("第一个接口--SC_OK--->"+HttpStatus.SC_OK);
if (resStatu == HttpStatus.SC_OK) {
HttpEntity entity = responce.getEntity();
bs.writeLog(entity);
reStr = EntityUtils.toString(entity);
bs.writeLog("进入归档初始化方法响应-->"+reStr);
jsonObject = JSONObject.parseObject(reStr);
String session = jsonObject.getString("session");
Integer code = jsonObject.getInteger("code");
if (0==code){
return session;
}
}
}catch (Exception e) {
e.printStackTrace();
}
return "f";
}
private static boolean processData(String depId,String doctype) throws IOException {
BaseBean bs = new BaseBean();
bs.writeLog("进入上传校验方法-->");
String upfile = ADDRESS_URL +"/oa/UploadFile.do";
HttpPost httppost = new HttpPost(upfile);
bs.writeLog("进入上传校验方法接口-->"+upfile);
bs.writeLog("进入上传校验方法--打印FileName-->"+FILENAME);
bs.writeLog("进入上传校验方法--打印Dept-->"+depId);
bs.writeLog("进入上传校验方法--打印doctype-->"+doctype);
MultipartEntityBuilder mEntityBuilder = MultipartEntityBuilder.create();
mEntityBuilder.addTextBody("FileName", FILENAME, ContentType.TEXT_PLAIN.withCharset("utf-8"));
mEntityBuilder.addTextBody("OffSoftName", "今易", ContentType.TEXT_PLAIN.withCharset("utf-8"));
mEntityBuilder.addTextBody("OffSoftVer", "V1.0", ContentType.TEXT_PLAIN.withCharset("utf-8"));
mEntityBuilder.addTextBody("Dept", depId, ContentType.TEXT_PLAIN.withCharset("utf-8"));//部门id
mEntityBuilder.addTextBody("doctype", doctype, ContentType.TEXT_PLAIN.withCharset("utf-8"));//0收文1发文
// mEntityBuilder.addTextBody("Strategy", "sw", ContentType.TEXT_PLAIN.withCharset("utf-8"));
String reStr;
JSONObject jsonObject;
HttpResponse responce;
httppost.setEntity(mEntityBuilder.build());
responce = httpClient.execute(httppost);
int resStatu = responce.getStatusLine().getStatusCode();
bs.writeLog("第三个接口--resStatu--->"+resStatu);
bs.writeLog("第三个接口--SC_OK--->"+HttpStatus.SC_OK);
if (resStatu == HttpStatus.SC_OK) {
// get result data
HttpEntity entity = responce.getEntity();
reStr = EntityUtils.toString(entity);
bs.writeLog("进入上传校验方法打印返回值-->"+reStr);
jsonObject = JSONObject.parseObject(reStr);
return 0 == jsonObject.getInteger("code");
} else {
return false;
}
}
public static boolean uploadFile(String filePath) throws Exception{
BaseBean bs = new BaseBean();
// String url = ADDRESS_URL +"/oa/UploadData.do";
// System.out.println("进入归档上传方法---session---->"+session);
// System.out.println("进入归档上传方法---服务端发布的接口地址---->"+url);
// System.out.println("进入归档上传方法---文件名称---->"+FILENAME);
// System.out.println("进入归档上传方法-->文件名称长度-->"+String.valueOf(file.length()));
// System.out.println("进入归档上传方法----需要传递到服务器的文件路径---"+filePath);//需要传递到服务器的文件路径
// File file = new File(filePath);
// ZipInputStream zipInputStream = null;
// InputStream inputStream = null;
// try {
// //InputStream in = new FileInputStream(file);
// if(!file.exists())
// {
// throw new WebApplicationException(404);
// }
// zipInputStream = new ZipInputStream(new FileInputStream(file));
// if (zipInputStream.getNextEntry() != null) {
// inputStream = new BufferedInputStream(zipInputStream);
// }
// MultipartEntityBuilder entityBuilder = MultipartEntityBuilder.create();
// entityBuilder.addTextBody("fileName", FILENAME);
// entityBuilder.addTextBody("fileSize", String.valueOf(file.length()));
// entityBuilder.addBinaryBody("file", inputStream,
// ContentType.MULTIPART_FORM_DATA, FILENAME);
// HttpPost httpPost = new HttpPost(url);
// httpPost.setEntity(entityBuilder.build());
// //执行post接口
// HttpResponse response = httpClient.execute(httpPost);
// String result="";
// if (response!=null && response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
// HttpEntity resEntity = response.getEntity();
// if(resEntity != null){
// result = EntityUtils.toString(resEntity, Consts.UTF_8);
// }
// }
// System.out.println("进入上传校验方法打印返回值-->"+result);
// JSONObject jsonObject = JSONObject.parseObject(result);
// return 0 == jsonObject.getInteger("code");
// } catch (FileNotFoundException e) {
// e.printStackTrace();
// } catch (ClientProtocolException e) {
// e.printStackTrace();
// } catch (IOException e) {
// e.printStackTrace();
// }
try {
String url = ADDRESS_URL +"/oa/UploadData.do";
HttpPost httppost = new HttpPost(url);
FileInputStream fis = new FileInputStream(file);
BufferedInputStream bis = new BufferedInputStream(fis);
int count = 1000000;
byte[] bytes = new byte[count];
int len = -1;
String reStr;
JSONObject jsonObject;
HttpResponse responce;
HttpEntity entity;
while ((len = bis.read(bytes, 0, count)) != -1) {
MultipartEntityBuilder mEntityBuilder = MultipartEntityBuilder.create();
mEntityBuilder.addTextBody("fileName", FILENAME, ContentType.TEXT_PLAIN.withCharset("utf-8"));
mEntityBuilder.addTextBody("fileSize", String.valueOf(len), ContentType.TEXT_PLAIN.withCharset("utf-8"));
mEntityBuilder.addBinaryBody("file", bytes, ContentType.DEFAULT_BINARY, FILENAME);
//mEntityBuilder.addTextBody("Dept", "市委", ContentType.TEXT_PLAIN.withCharset("utf-8"));
httppost.setEntity(mEntityBuilder.build());
responce = httpClient.execute(httppost);
int resStatu = responce.getStatusLine().getStatusCode();
if (resStatu == HttpStatus.SC_OK) {
// get result data
entity = responce.getEntity();
reStr = EntityUtils.toString(entity);
jsonObject = JSONObject.parseObject(reStr);
bs.writeLog("reStr->"+reStr);
if (0 != jsonObject.getInteger("code")) {
bs.writeLog("上传出错");
return false;
}
} else {
bs.writeLog("上传出错");
return false;
}
}
bs.writeLog("上传完成");
return true;
} finally {
bs.writeLog("上传完成");
}
}
public static int doit(String filename,String filepath ,String doctype,String depId) throws Exception{
BaseBean bs = new BaseBean();
bs.writeLog("点击归档按钮-->filename-->"+filename);
bs.writeLog("点击归档按钮-->filepath-->"+filepath);
int success =9;
DaHttpAction HttpAction = new DaHttpAction();
HttpAction.setFILENAME(filename);
HttpAction.setFile(new File(filepath));
bs.writeLog("设置文件名称-->"+FILENAME);
bs.writeLog("设置文件大小-->"+String.valueOf(file.length()));
try {
String session = init();
if ("f".equals(session)) {
bs.writeLog("初始化出错");
return 2;
}else {
bs.writeLog("第一个接口执行成功-->文件初始化成功-->session-->"+session);
if (!uploadFile(filepath)) {
bs.writeLog("文件上传出错");
return 4;
}else {
bs.writeLog("第二个接口执行成功-->文件上传成功");
bs.writeLog("上传成功");
if (!processData(depId,doctype)) {
bs.writeLog("处理失败");
return 5;
} else {
success=0;
bs.writeLog("第三个接口执行成功-->文件验证成功");
}
}
}
} finally {
success=0;
// httpClient.close();
}
return success;
}
}