package jp.co.unisys.energy.bems.controller.csv;
import static org.junit.Assert.assertEquals;
import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.Closeable;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.text.MessageFormat;
import org.apache.commons.io.IOUtils;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest
public class CsvFileReadTest {
static final String BOUNDARY = "#";
static final String NEWLINE = "\r\n";
static final String PREFIX = "--";
// 新
static final String SHIJISU_URL = "http://localhost:8888/bems/api/shijisu";
// static final String SHIJISU_URL = "http://localhost:8080/bems/api/shijisu/index";
static final String ENERGY_URL = "http://localhost:8888/bems/api/energy";
// 旧
// static final String ENERGY_URL = "http://localhost:8080/bems/api/energy/index";
static final String TOKO_ENRGY1_CSV_PATH = "D:\\Local_SVN\\03_プロジェクト開発\\99_不具合調査\\bak\\TOKO_enrgy{0}.csv";
static final String TOKO_SHIJISU_CSV_PATH = "D:\\Local_SVN\\03_プロジェクト開発\\99_不具合調査\\bak\\TOKO_SHIJISU.csv";
static final String NAME = "csvFile";
@Test
public void csvRead() throws IOException {
for(int i = 1; i < 20 ;i++) {
newThead(ENERGY_URL, MessageFormat.format(TOKO_ENRGY1_CSV_PATH, i));
}
// newThead(ENERGY_URL, MessageFormat.format(TOKO_ENRGY1_CSV_PATH, 15));
// newThead(ENERGY_URL, MessageFormat.format(TOKO_ENRGY1_CSV_PATH, 2));
// newThead(ENERGY_URL, MessageFormat.format(TOKO_ENRGY1_CSV_PATH, 3));
// for(int i = 1; i < 20 ;i++) {
// }
newThead(SHIJISU_URL,TOKO_SHIJISU_CSV_PATH);
// newThead(SHIJISU_URL,TOKO_SHIJISU_CSV_PATH);
// newThead(SHIJISU_URL,TOKO_SHIJISU_CSV_PATH);
}
public static void newThead(String url, String csvPath) {
new Thread(()->{
try {
csvFileRead(url,csvPath);
} catch (IOException e) {
System.out.println("线程出现问题");
e.printStackTrace();
}
}).start();
}
public static void csvFileRead(String url,String csvPath) throws IOException {
HttpURLConnection httpConn = null;
BufferedInputStream bis = null;
DataOutputStream dos = null;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
InputStream ins = null;
byte[] body_data = null;
try {
ins = new FileInputStream(csvPath);
body_data = IOUtils.toByteArray(ins);
} catch (IOException e1) {
e1.printStackTrace();
throw e1;
} finally {
ioClose(ins);
}
try {
httpConn = httpUrl(httpConn,url);
dos = new DataOutputStream(httpConn.getOutputStream());
if (body_data != null && body_data.length > 0) {
dos.writeBytes(PREFIX + BOUNDARY + NEWLINE);
String fileName = csvPath.substring(csvPath.lastIndexOf(File.separatorChar) + 1);
dos.writeBytes("Content-Disposition: form-data; " + "name=\"" + NAME + "\"; filename=\"" + fileName + "\"" + NEWLINE);
dos.writeBytes(NEWLINE);
dos.write(body_data);
dos.writeBytes(NEWLINE);
}
dos.writeBytes(PREFIX + BOUNDARY + PREFIX + NEWLINE);
byte[] buffer = new byte[8 * 1024];
int c = 0;
System.out.println("返回状态" + httpConn.getResponseCode());
assertEquals(200, httpConn.getResponseCode());
if (httpConn.getResponseCode() == 200) {
bis = new BufferedInputStream(httpConn.getInputStream());
while ((c = bis.read(buffer)) != -1) {
baos.write(buffer, 0, c);
baos.flush();
}
}
System.out.println(new String(baos.toByteArray(), "utf-8"));
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
ioClose(dos,bis,baos);
}
}
private static HttpURLConnection httpUrl(HttpURLConnection httpConn, String url) throws IOException {
URL urlObj = new URL(url);
httpConn = (HttpURLConnection) urlObj.openConnection();
httpConn.setDoInput(true);
httpConn.setDoOutput(true);
httpConn.setRequestMethod("POST");
httpConn.setUseCaches(false);
httpConn.setRequestProperty("Connection", "Keep-Alive");
httpConn.setRequestProperty("Accept", "*/*");
httpConn.setRequestProperty("Accept-Encoding", "gzip, deflate");
httpConn.setRequestProperty("Cache-Control", "no-cache");
httpConn.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + BOUNDARY);
httpConn.setRequestProperty("User-Agent",
"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 2.0.50727; .NET CLR 3.0.04506.30)");
httpConn.connect();
return httpConn;
}
private static void ioClose(Closeable... closeables) {
if(closeables!=null) {
for(Closeable c:closeables) {
if (c != null) {
try {
c.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
}