我现在的项目有个功能是要在本地发送xml文件到servlet,这个过程是我主动请求的,servlet得到请求后接收到xml文件,然后把反馈信息(result.xml)发给我,我然后再接收这个result.xml文件,判断操作是否成功。
HttpURLConnecion connection = null;
/**
* 初始化
*
*/
public void init(String urlString){
try {
if(urlString !=null)
url = new URL(urlString);
if(url !=null)
connection = (HttpURLConnection)url.openConnection();
connection.setDoOutput(true);
connection.setRequestProperty("Content-Type","multipart/form-data");
} catch (MalformedURLException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
} catch (IOException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}
}
public void sendData(String fileName,String filePath){
String urlPath = "http://ip:7001/pack/acceptServlet?username=sys&password=sys";
init(urlPath);
try {
DataInputStream fis = new DataInputStream(new BufferedInputStream(new FileInputStream(filePath)));//本地xml文件
DataOutputStream ps = new DataOutputStream(new DataOutputStream(connection.getOutputStream()));
int bufferSize = 8192;
byte[] buf = new byte[bufferSize];
while (true) {
int read = 0;
if (fis != null) {
read = fis.read(buf);
}
if (read == -1) {
break;
}
ps.write(buf, 0, read);
}
ps.flush();
fis.close();
ps.close();
System.out.println("文件传输完成");
//开始接收Result文件
acceptResult("F:\\result.xml");
} catch (IOException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}
}
public void acceptResult(String path){
try {
int passedlen = 0;
byte[] buf = new byte[8192];
DataInputStream inputStream = new DataInputStream(new BufferedInputStream(
connection.getInputStream()));
DataOutputStream fileOut = new DataOutputStream(
new BufferedOutputStream(new BufferedOutputStream(
new FileOutputStream(path))));
while (true) {
int read = 0;
if (inputStream != null) {
read = inputStream.read(buf);
}
passedlen += read;
if (read == -1) {
break;
}
fileOut.write(buf, 0, read);
}
System.out.println("保存完成");
fileOut.flush();
fileOut.close();
} catch (FileNotFoundException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
} catch (IOException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}
}
以下是servlet代码:
public class AcceptTestServlet extends HttpServlet{
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO 自动生成方法存根\
// File file = new File(path);
DataInputStream dis = new DataInputStream(
new BufferedInputStream(request.getInputStream()));
System.out.println("开始接收文件");
DataOutputStream fileOut = new DataOutputStream(
new BufferedOutputStream(new BufferedOutputStream(
new FileOutputStream("F:\\projectInfo.xml"))));
int bufferSize = 8192;
byte[] buf = new byte[bufferSize];
while (true) {
int read = 0;
if (dis != null) {
read = dis.read(buf);
}
if (read == -1) {
break;
}
fileOut.write(buf, 0, read);
}
fileOut.flush();
dis.close();
fileOut.close();
System.out.println("文件接收完成");
// 开始返回结果文件
String path= "";
if(request.getParameter("isupdate") != null){
String isupdate = request.getParameter("isupdate");
if("sys".equalsIgnoreCase(username) && "sys".equalsIgnoreCase(password) ){
path = "F:\\success_Result.xml";
}else
path = "F:\\fail_Result.xml";
}
System.out.println("path:"+path);
System.out.println("开始返回结果文件");
DataInputStream fis = new DataInputStream(
new BufferedInputStream(new FileInputStream(path)));
OutputStream os = response.getOutputStream();
DataOutputStream dos = new DataOutputStream(os);
byte[] b = new byte[8192];
while (true) {
int read = 0;
if (fis != null) {
read = fis.read(b);
}
if(read == -1){
break;
}
dos.write(b,0,read);
}
dos.flush();
fis.close();
os.close();
dos.close();
System.out.println("返回结果文件完毕");
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO 自动生成方法存根
doGet(request,response);
}
}
当我执行到acceptResult方法中的DataInputStream inputStream = new DataInputStream(new BufferedInputStream(
connection.getInputStream()))时,就报了一个异常:java.net.SocketException: Software caused connection abort: recv failed;并且AcceptTestServlet 还没有执行,因为只有当connection.getInputStream()执行的时候,AcceptTestServlet 才会有响应;
我当时想,是不是要重新连接一次才行,后来重新open了一次后就没报错了,但是AcceptTestServlet 却不能收到文件;因为这是两个不同的连接,servlet当然读不到这个流
所以说,必须还是得在同一个连接下才行,关键就是这个异常,不知道是什么意思,真是郁闷死我了。有哪位高人做过这方面的东西,能否指点一二呢?