这是报错日志,发送一个文件到目标端。之前一直都可以,但是今天一样的程序却发送失败了,求解答。
代码如下:
package com.guoxi;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
/**
* created by hangli
*/
public class SendFile {
public static void main(String[] args){
try{
/**
* 发送序号(序号比编号少1)
*/
int order = 30 ;
URL startseries = new URL("http://172.28.165.229:8080/startReceive?seri="+order);
InputStream start = startseries.openStream();
InputStreamReader st = new InputStreamReader(start);
BufferedReader bustart = new BufferedReader(st);
bustart.close();
st.close();
start.close();
/**
* 发送文件
*/
String boundary = "Boundary-b1ed-4060-99b9-fca7ff59c113"; //Could be any string
String Enter = "\r\n";
File file = new File("/home/admin/demo/hangli/filename31_31.txt");
FileInputStream fis = new FileInputStream(file);
URL url = new URL("http://172.28.165.229:8080/uploadimg");
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
conn.setDoOutput(true);
conn.setDoInput(true);
conn.setRequestMethod("POST");
conn.setUseCaches(false);
conn.setInstanceFollowRedirects(true);
conn.setRequestProperty("Content-Type","multipart/form-data;boundary=" + boundary);
conn.connect();
DataOutputStream dos = new DataOutputStream(conn.getOutputStream());
//part 1
String part1 = "--" + boundary + Enter
+ "Content-Type: application/octet-stream" + Enter
+ "Content-Disposition: form-data; filename=\""+file.getName()+"\"; name=\"file\"" + Enter + Enter;
//part 2
String part2 = Enter
+ "--" + boundary + Enter
+ "Content-Type: text/plain" + Enter
+ "Content-Disposition: form-data; name=\"dataFormat\"" + Enter + Enter
+ "hk" + Enter
+ "--" + boundary + "--";
byte[] xmlBytes = new byte[fis.available()];
fis.read(xmlBytes);
dos.writeBytes(part1);
dos.write(xmlBytes);
dos.writeBytes(part2);
dos.flush();
dos.close();
fis.close();
System.out.println("status code: "+conn.getResponseCode());
conn.disconnect();
}catch(Exception e){
e.printStackTrace();
}
}
}
看下目标端 程序 及端口是不是出问题了.
你的目标端是 主机172.28.165.229的 8080 ?
在你本地 telnet 测试连接目标的 8080 , 或者ssh指定目标端口 测试连接, 看对端8080是否正常.
收起