Axis2 文件按块上传

发布于 2024-10-02 17:52:23 字数 1744 浏览 4 评论 0原文

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

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

江南烟雨〆相思醉 2024-10-09 17:52:23

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

更改

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

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

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 )
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文