linux环境下执行HttpURLConnection发送图片流失败

iljsan 发布于 2012/12/21 15:07
阅读 1K+
收藏 0

大侠们,您们好,小弟有个问题问下:

我现在使用HttpURLConnection进行发送图片流到某服务器上,在window的环境下是通过的,但是部署到linux中,就无法通过,由于是破解的某网站的服务,所有没有报错信息可看。

代码如下:

String multipart_form_data = "multipart/form-data";


String twoHyphens = "--";


String boundary = "------WebKitFormBoundaryyMLsnEy0frudsuEL"; // 数据分隔符


String lineEnd = System.getProperty("line.separator"); // The value is
// "\r\n" in
// Windows.


/*

* 上传图片内容,格式请参考HTTP 协议格式。

* 格式如下所示:
* ------WebKitFormBoundaryyMLsnEy0cxZfPOEL
* --****************fD4fH3hK7aI6

* Content-Disposition: form-data; name="upload_file"; filename=".jpg"

* Content-Type: image/jpeg



* 这儿是文件的内容,二进制流的形式
*/


private void addImageContent(Image[] files, DataOutputStream output) throws UnsupportedEncodingException {


for (Image file : files) {


StringBuilder split = new StringBuilder();


split.append(twoHyphens + boundary + lineEnd);


split.append("Content-Disposition: form-data; name=\""
+ file.getFormName() + "\"; filename=\""
+ file.getFileName() + "\"" + lineEnd);


split.append("Content-Type: " + file.getContentType() + lineEnd);


split.append(lineEnd);


try {


// 发送图片数据


output.writeBytes(split.toString());
output.write(file.getData(), 0, file.getData().length);
output.writeBytes(lineEnd);
} catch (IOException e) {
e.printStackTrace();
throw new RuntimeException(e);


}


}


}


/*

* 构建表单字段内容,格式请参考HTTP 协议格式(用FireBug可以抓取到相关数据)。(以便上传表单相对应的参数值)

* 格式如下所示:

* --****************fD4fH3hK7aI6

* Content-Disposition: form-data; name="action"

* // 一空行,必须有

* upload
*/


private void addFormField(DataOutputStream output) {


StringBuilder sb = new StringBuilder();


sb.append(twoHyphens + boundary + lineEnd);


sb.append("Content-Disposition: form-data; name=\"formId\"" + lineEnd);


sb.append(lineEnd);


System.out.println(sb.toString());
try {


output.writeBytes(sb.toString());// 发送表单字段数据


} catch (IOException e) {
e.printStackTrace();
throw new RuntimeException(e);


}


}


/**

* 直接通过HTTP 协议提交数据到服务器,实现表单提交功能。

* @param actionUrl
*            上传路径

* @param params
*            请求参数key为参数名,value为参数值

* @param files
*            上传文件信息

* @return 返回请求结果
*/


public String uploadImage(String actionUrl, Image[] files ,String cookieStr) {
HttpClient client = new DefaultHttpClient();  
         
         
         
   client.getConnectionManager().closeIdleConnections(120, TimeUnit.SECONDS);//20秒 
HttpURLConnection conn = null;
DataOutputStream output = null;
BufferedReader input = null;
try {
URL url = new URL(actionUrl);
conn = (HttpURLConnection) url.openConnection();
conn.setConnectTimeout(120000);
conn.setDoInput(true); // 允许输入
conn.setDoOutput(true); // 允许输出
conn.setUseCaches(false); // 不使用Cache
conn.setRequestMethod("POST");
conn.setRequestProperty("Connection", "keep-alive");
conn.setRequestProperty("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
conn.setRequestProperty("Accept-Charset", "GBK,utf-8;q=0.7,*;q=0.");
conn.setRequestProperty("Accept-Encoding", "gzip,deflate,sdch");
conn.setRequestProperty("Accept-Language", "zh-CN,zh;q=0.8");
conn.setRequestProperty("Cache-Control", "max-age=0");
conn.setRequestProperty("Connection", "keep-alive");
conn.setRequestProperty("Host", "********");
conn.setRequestProperty("Content-Length", String.valueOf(files[0].getData().length));
conn.setRequestProperty("Content-Type",  
                 "multipart/form-data;boundary=" + boundary); 
conn.setRequestProperty("Charset", "UTF-8"); 
conn.setRequestProperty("Referer", "********");
conn.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.1 (KHTML, like Gecko) Chrome/21.0.1180.89 Safari/537.1");
conn.setRequestProperty("Cookie", cookieStr);
conn.connect();
output = new DataOutputStream(conn.getOutputStream());
addImageContent(files, output); // 添加图片内容
addFormField(output); // 添加表单字段内容
output.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);// 数据结束标志
output.flush();
if (output != null) {
output.close();
}
int code = conn.getResponseCode();
if (code != 200) {
throw new RuntimeException("请求‘" + actionUrl + "’失败!");
}
input = new BufferedReader(new InputStreamReader( conn.getInputStream() ,"utf-8"));
StringBuilder response = new StringBuilder();
String oneLine;
while ((oneLine = input.readLine()) != null) {
response.append(oneLine + lineEnd);
}
return response.toString();
} catch (IOException e) {
e.printStackTrace();
throw new RuntimeException(e);
} finally {
// 统一释放资源
try {

if (input != null) {
input.close();
}


} catch (IOException e) {
throw new RuntimeException(e);
}
if (conn != null) {
conn.disconnect();
}
}
}


加载中
返回顶部
顶部