目的:
从C#创建的客户端中向Java创建的servlet提交请求(不知道这是否算作严格的CS架构),servet返回一个文件流( 任意格式的文件 ),然后通过C#获取该文件流并保存到本地,使得文件可以正常打开并完整无缺。
发现问题:
- Java中的servlet代码如下:
package servlet;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
@WebServlet(urlPatterns = "/update")
public class getUpdate extends HttpServlet{
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// super.doGet(req, resp);
File file=new File("D:\\test.exe");
FileInputStream inputStream=new FileInputStream(file);
int i=inputStream.available();
byte[] data=new byte[i];
inputStream.read(data);
ServletOutputStream outputStream=resp.getOutputStream();
outputStream.write(data);
outputStream.flush();
outputStream.close();
inputStream.close();
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
super.doPost(req, resp);
}
}
servlet的环境为本地,D盘下的test.exe是一个可执行的winform程序,用于文件的完整性测试。
将以上代码部署到tomcat后,直接在浏览器中输入“http://localhost:8080/update”可以下载到名为update的文件,给update添加.exe后缀后直接打开发现可以正常执行,这表明该servlet并没什么问题。
- 客户端的C#代码如下:
using System;
using System.IO;
using System.Net;
namespace ServiceTest
{
class Program
{
static void Main(string[] args)
{
string url = "http://localhost:8080/update";
HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url);
WebResponse webResponse = webRequest.GetResponse();
Stream responseStream = webResponse.GetResponseStream();
FileStream fileStream = new FileStream("test.exe", FileMode.Create);
byte[] data = new byte[100000];
int count = responseStream.Read(data, 0, data.Length);
fileStream.Write(data, 0, count);
Console.WriteLine("OK");
fileStream.Close();
responseStream.Close();
}
}
}
刚开始我觉得test.exe文件不过只有15kb,这里给出的byte[100000]一定绰绰有余。所以当我执行程序时,发现控制台可以正常输出“OK”,但是在该项目目录下的test.exe文件却只有8kb:
test.exe属性
而且很明显,双击该文件无法执行:
test.exe执行结果
解决问题:
- 然后我用Java写了个测试代码:
package test;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
public class Test2 {
public static void main(String[] args) {
String url = "http://localhost:8080/update";
try {
URLConnection connection = new URL(url).openConnection();
HttpURLConnection httpURLConnection = (HttpURLConnection) connection;
InputStream inputStream = httpURLConnection.getInputStream();
int i = inputStream.available();
byte[] data = new byte[i];
inputStream.read(data);
File file = new File("D:\\test2.exe");
OutputStream outputStream = new FileOutputStream(file);
outputStream.write(data);
System.out.println("finish");
outputStream.close();
inputStream.close();
httpURLConnection.disconnect();
((HttpURLConnection) connection).disconnect();
} catch (Exception e) {
e.printStackTrace();
}
}
}
运行后得到的test2.exe文件完全正常!
-
于是接下来分别对C#客户端代码和Java客户端代码进行调试:
C#调试
Java调试
从上面两张图可以看出data内容不尽相同,在C#中data[2]=144,但是在Java中data[2]=-112。原因是C#中的byte取值为0~255,而Java中的byte取值为-128~127。但这好像不是问题的关键,关键是在C#中的8192行以后,数据全部为空,到此截断。
C#调试
掐指一算,8192/1024=8。这时才明白new byte[100000]的写法是错误的,而且是及其荒谬可笑的不符合程序员思想的不能容忍的应当被枪毙的错误!
- 修正后的C#关键代码如下:
byte[] data = new byte[1024];
int count;
while ((count = responseStream.Read(data, 0, data.Length)) != 0)
{
fileStream.Write(data, 0, count);
}
- 同时意识到servlet中的写法也是不负责任的,当要传输的文件过大时,问题就会显现出来,因此修改后的servlet关键代码如下:
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
File file=new File("D:\\test.exe");
FileInputStream inputStream=new FileInputStream(file);
ServletOutputStream outputStream=resp.getOutputStream();
byte[] data=new byte[1024];
int count;
while ((count=inputStream.read(data,0,data.length))!=-1){
outputStream.write(data,0,count);
}
outputStream.flush();
outputStream.close();
inputStream.close();
}
网友评论