图片上传
package com.testutil;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
public class ImageUploadTestByWg {
@SuppressWarnings("finally")
public static String uploadFile(String actionUrl, String[] uploadFilePaths) {
String end = "\r\n";
String twoHyphens = "--";
String boundary = "*****";
DataOutputStream ds = null;
InputStream inputStream = null;
InputStreamReader inputStreamReader = null;
BufferedReader reader = null;
StringBuffer resultBuffer = new StringBuffer();
String tempLine = null;
try {
URL url = new URL(actionUrl);
URLConnection urlConnection = url.openConnection();
HttpURLConnection httpURLConnection = (HttpURLConnection) urlConnection;
httpURLConnection.setDoInput(true);
httpURLConnection.setDoOutput(true);
httpURLConnection.setUseCaches(false);
httpURLConnection.setRequestMethod("POST");
httpURLConnection.setRequestProperty("Connection", "Keep-Alive");
httpURLConnection.setRequestProperty("Charset", "UTF-8");
httpURLConnection.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + boundary);
ds = new DataOutputStream(httpURLConnection.getOutputStream());
for (int i = 0; i < uploadFilePaths.length; i++) {
String uploadFile = uploadFilePaths[i];
String filename = uploadFile.substring(uploadFile.lastIndexOf("//") + 1);
ds.writeBytes(twoHyphens + boundary + end);
ds.writeBytes("Content-Disposition: form-data; " + "name=\"file" + i + "\";filename=\"" + filename
+ "\"" + end);
ds.writeBytes(end);
FileInputStream fStream = new FileInputStream(uploadFile);
int bufferSize = 1024;
byte[] buffer = new byte[bufferSize];
int length = -1;
while ((length = fStream.read(buffer)) != -1) {
ds.write(buffer, 0, length);
}
ds.writeBytes(end);
fStream.close();
}
ds.writeBytes(twoHyphens + boundary + twoHyphens + end);
ds.flush();
if (httpURLConnection.getResponseCode() >= 300) {
throw new Exception(
"HTTP Request is not success, Response code is " + httpURLConnection.getResponseCode());
}
if (httpURLConnection.getResponseCode() == HttpURLConnection.HTTP_OK) {
inputStream = httpURLConnection.getInputStream();
inputStreamReader = new InputStreamReader(inputStream);
reader = new BufferedReader(inputStreamReader);
tempLine = null;
resultBuffer = new StringBuffer();
while ((tempLine = reader.readLine()) != null) {
resultBuffer.append(tempLine);
resultBuffer.append("\n");
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (ds != null) {
try {
ds.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (inputStreamReader != null) {
try {
inputStreamReader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (inputStream != null) {
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return resultBuffer.toString();
}
}
public static void main(String[] args) {
String str = uploadFile("http://10.0.100.17:6013/ImageExt/uploadImage?app_code =GIS_C_11",
new String[] { "e://129900749.zip" });
System.out.println(str);
}
}
图片下载
package com.testutil;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.params.HttpMethodParams;
public class ImageDownLoadTest {
public void test2() throws Exception {
String urlStr = "http://10.0.100.17:6013/ImageExt/getImageList?app_code=GIS_C_11&busi_no=07I1010200111102018000002";
GetMethod getMethod = new GetMethod(urlStr);
getMethod.getParams().setContentCharset("utf-8");
HttpClient client = new HttpClient();
client.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET,
"utf-8");
client.getHttpConnectionManager().getParams().setConnectionTimeout(
50000);
int statusCode = client.executeMethod(getMethod);
if (statusCode != HttpStatus.SC_OK) {
System.out.println(getMethod.getStatusLine());
throw new IllegalStateException("Method failed: "
+ getMethod.getStatusLine());
}
InputStream inputStream = getMethod.getResponseBodyAsStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(
inputStream, "UTF-8"));
String strMessage = null;
StringBuffer buffer = new StringBuffer();
while ((strMessage = reader.readLine()) != null) {
buffer.append(strMessage);
}
System.out.println(buffer.toString());
getMethod.releaseConnection();
}
public void test3() throws Exception {
String urlStr = "http://10.0.100.17:6013/ImageExt/viewImage?path=/TRMTP/GIS_C_11/2018/03/09/78/74/62fd395f17bf3297a5a57e74774dc121_1/6fb934ba-a8b0-4f8f-9968-85be6ee296f6.jpg";
GetMethod getMethod = new GetMethod(urlStr);
getMethod.getParams().setContentCharset("utf-8");
HttpClient client = new HttpClient();
client.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET,
"utf-8");
client.getHttpConnectionManager().getParams().setConnectionTimeout(
50000);
int statusCode = client.executeMethod(getMethod);
if (statusCode != HttpStatus.SC_OK) {
System.out.println(getMethod.getStatusLine());
throw new IllegalStateException("Method failed: "
+ getMethod.getStatusLine());
}
InputStream inputStream = getMethod.getResponseBodyAsStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(
inputStream, "UTF-8"));
String strMessage = null;
StringBuffer buffer = new StringBuffer();
while ((strMessage = reader.readLine()) != null) {
buffer.append(strMessage);
}
System.out.println(buffer.toString());
getMethod.releaseConnection();
}
}