Axis2文件按块上传 [英] Axis2 File Upload by chunk
问题描述
我正在尝试使用Axis2 Web服务以1024块大小上传文件.
I'm trying to upload file using Axis2 web service by 1024 chunk size.
我的服务器端看起来像这样:
My server side looks like this:
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();
}
}
}
我的客户端看起来像这样:
my client side looks like this:
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之间变化.
After it the file size is incorrect and if origina file size is 500 Kb, the original size varies between 200 and 400k.
我做错了什么?
更新:我查看了Tomcat中的log4j文件
Update: I looked at log4j file in Tomcat
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异常,该文件已由其他进程使用.
It looks like all requests to the web server are done Asynchronously and and I also getting IO exception that the file is used by another process.
推荐答案
尝试在服务器实现中的fos.close();
之前添加fos.flush();
.
try to add fos.flush();
before your fos.close();
in your server implementation.
更改
while((count = fis.read(buffer, 0, CHUNK_SIZE)) >0 )
为
while((count = fis.read(buffer, 0, CHUNK_SIZE)) != -1 )
这篇关于Axis2文件按块上传的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!