简体   繁体   中英

Axis2 File Upload by chunk

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();
        }
}

After it the file size is incorrect and if origina file size is 500 Kb, the original size varies between 200 and 400k.

What am I doing wrong?

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

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.

try to add fos.flush(); before your fos.close(); in your server implementation.

Change

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

for

while((count = fis.read(buffer, 0, CHUNK_SIZE)) != -1 )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM