【发布时间】:2011-05-11 09:16:19
【问题描述】:
我正在尝试使用 Axis2 Web 服务按 1024 块大小上传文件。
我的服务器端是这样的:
public void appendChunk(int count, byte[] buffer){
FileOutputStream fos = null;
try {
File destinationFile = new File("c:\\file1.exe");
fos = new FileOutputStream(destinationFile,true);
fos.write(buffer,0, count);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
finally{
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
我的客户端如下所示:
static int CHUNK_SIZE =1024;
public static void main(String[] args) throws IOException, ServiceException {
FileUploadService strub = new FileUploadServiceLocator();
FileUploadServicePortType a = strub.getFileUploadServiceHttpSoap12Endpoint();
byte[] buffer = new byte[CHUNK_SIZE];
FileInputStream fis = null;
File file = new File("C:\\install.exe");
int count;
try {
fis = new FileInputStream(file);
while((count = fis.read(buffer, 0, CHUNK_SIZE)) >0 )
{
a.appendChunk(count, buffer);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
finally{
fis.close();
}
}
之后文件大小不正确,如果原始文件大小为 500 Kb,则原始大小在 200 到 400k 之间变化。
我做错了什么?
更新:我查看了 Tomcat 中的 log4j 文件
Nov 17, 2010 2:08:31 PM org.apache.tomcat.util.net.JIoEndpoint createWorkerThread
INFO: Maximum number of threads (200) created for connector with address null and port 80
看起来对 Web 服务器的所有请求都是异步完成的,而且我还收到文件被另一个进程使用的 IO 异常。
【问题讨论】:
-
您为此使用网络服务有什么具体原因吗?这将产生非常高的开销,因为您的数据将在嵌入 xml 之前转换为 base64,每个块也可能使用新的 TCP 连接。为什么不使用一个简单的 http post 请求来代替 servlet?
-
嗨,不幸的是,有一个原因,我有第三方客户端以这种方式上传二进制数据:(我将不得不编写与该客户端兼容的 Web 服务。我明白我'所有 SOAP 标头都会有性能问题,但老实说我不在乎。
标签: java web-services axis2 java-io