[英]Axis2 File Upload by chunk
我正在嘗試使用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異常,該文件被另一個進程使用。
嘗試添加fos.flush();
在你的fos.close();
之前fos.close();
在您的服務器實現中。
更改
while((count = fis.read(buffer, 0, CHUNK_SIZE)) >0 )
對於
while((count = fis.read(buffer, 0, CHUNK_SIZE)) != -1 )
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.