java编写Web Service,含大文件上传例子


参考文献:http://www.eclipse.org/webtools/community/tutorials/BottomUpAxis2WebService/bu_tutorial.html

这篇文章写的非常齐全,唯一在问题出在启动服务器后找不到网页,正确的网址应该是

http://localhost:8080/Axis2WSTest/axis2-web

java编写Web Service代码,含文件大文件上传例子

下载:http://code.google.com/p/minioa/downloads/detail?name=Axis2WSTest.zip&can=2&q=

基本步骤:

1、首先创建一个Web项目

2、添加axis视图

3、创建一个java类

4、选择这个java,然后New 创建一个web service,按照向导配置

5、run as ......

上传单个文件代码,值得注意的是当上传文件超过5M时就提示java.lang.outmemoryerror,需要就要考虑分割上传文件,到服务器端再合并

 

 

public void uploadImage(String filename, byte[] file) {
    try {
        FileOutputStream fos = null;
        fos = new FileOutputStream(filename);   
        fos.write(file);
        fos.close();
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}

 

合并文件

 

public int buildFile(String fileName,int num) {
    try{
        int i = 0;
        FileOutputStream fos = null;
        fos = new FileOutputStream(fileName); 
        while(num >=0){
            BufferedInputStream bufferedInputStream=new BufferedInputStream(new FileInputStream(fileName + i));
            byte[] bytes = new byte[(int)(new java.io.File(fileName + i)).length()];
            bufferedInputStream.read(bytes);
            bufferedInputStream.close();
            fos.write(bytes);
            num--;
            i++;
        }
        fos.close();
        //删除文件
        while(i >=0){
            java.io.File f= new java.io.File(fileName + i);
            f.delete();
            i--;
        }
        return 1;
    }catch(Exception ex){ex.printStackTrace();}
    return -1;
}

客户端是用.net winform写的

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM