代码之家  ›  专栏  ›  技术社区  ›  danny.lesnik

按块上传Axis2文件

  •  3
  • danny.lesnik  · 技术社区  · 15 年前

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

    1 回复  |  直到 15 年前
        1
  •  1
  •   Donal Fellows    15 年前

    尝试添加 fos.flush(); 在你的 fos.close();

    改变

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

    对于

    while((count = fis.read(buffer, 0, CHUNK_SIZE)) != -1 )
    
    推荐文章