3

我正在尝试使用 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 异常。

4

1 回答 1

1

尝试在您的服务器实现fos.flush();之前添加。fos.close();

改变

while((count = fis.read(buffer, 0, CHUNK_SIZE)) >0 )

为了

while((count = fis.read(buffer, 0, CHUNK_SIZE)) != -1 )
于 2011-05-26T08:10:43.350 回答