webservice 文件上传删除

创建webservice 服务端:

package com.fileup.upservice;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
 * 文件上传webservice
 * 
 * @author root
 * 
 */
public class FileUploadService {
 /**
  * 设置上传文件所需参数
  * 
  * @param declare
  * @param endName
  * @param outputStream
  * @return
  */
 public String uploadFile(String declare, String endName, byte[] outputStream) {
    Date date = new Date();
    SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
    String dateStr = sdf.format(date);
    FileOutputStream out = null;
    String filePath = this.getClass().getClassLoader().getResource("/")
    .getPath();
    //可以通过输出路径查看文件的上传路径
    System.out.println("################" + filePath + "#################");
    filePath = filePath.substring(1, filePath.length());
    String realUrl = filePath + "upload";
    if (!new File(realUrl).isDirectory())
    new File(realUrl).mkdirs();
    try {
    String fileName = realUrl + "/" + dateStr + endName;
    out = new FileOutputStream(fileName);// 上传文件存放路径
    out.write(outputStream, 0, outputStream.length);
    } catch (Exception e) {
    System.out.println("提示信息:" + this.getClass().getName() + "|"
     + e.getMessage());
   return "{success:false}";
   } finally {
   if (out != null) {
    try {
     out.close();
    } catch (IOException e) {
     // TODO Auto-generated catch block
     System.out.println("提示:" + e.getMessage());
     }
    }
  }
   return "{success:true}";
 }
   /**
    * 删除文件方法
    * @param filePath
    * @return
    */
 public String deleteFile(String filePath) {
    File file = new File(filePath);
    return file.delete()?"{success:true}":"{success:false}";
 }
}

上传测试代码

package com.fileup.upfileclient;
import java.io.File;
import java.io.FileInputStream;
import java.util.Date;
import javax.xml.namespace.QName;
import org.apache.axis2.addressing.EndpointReference;
import org.apache.axis2.client.Options;
import org.apache.axis2.rpc.client.RPCServiceClient;
import com.fileup.util.UtilFile;
/**
 * webservice 文件上传测试代码
 * 
 * @author root
 * 
 */
public class FileUploadClient {
 
 public static void main(String[] args) throws Exception {
  //指定要上传文件的路径
     String fileName = "C:\\Users\\root\\Desktop\\activation-1.1.jar";
     RPCServiceClient sc = new RPCServiceClient();
     Options options = sc.getOptions();
     EndpointReference erf = new EndpointReference(
     "http://localhost:8088/FileUploadInfo/services/upload_service");
     options.setTo(erf);
     File file = new File(fileName);
     FileInputStream in = new FileInputStream(file);
     byte[] bs = new byte[in.available()];
     in.read(bs);
     Object[] opAddEntryArgs = new Object[] { "测试上传", UtilFile.fileEndName(fileName) ,bs };
     if (in != null) {
       in.close();
   }
     // 设置返回值类型
     Class<?>[] classes = new Class<?>[] { String.class };
     // 命名空间和方法名称
     QName qname = new QName("http://upservice.fileup.com", "uploadFile");
     // 执行文件上传,接收返回值
     Object returnValue = sc.invokeBlocking(qname,
     opAddEntryArgs, classes)[0];
     System.out.println(new Date() + " 文件上传结束,返回值:" + returnValue);
  }
}

删除测试代码

package com.fileup.upfileclient;
import java.util.Date;
import javax.xml.namespace.QName;
import org.apache.axis2.addressing.EndpointReference;
import org.apache.axis2.client.Options;
import org.apache.axis2.rpc.client.RPCServiceClient;
/**
 * webservice 文件上传测试代码
 * 
 * @author root
 * 
 */
public class FileUploadClient2 {
 
 public static void main(String[] args) throws Exception {
     RPCServiceClient sc = new RPCServiceClient();
     Options options = sc.getOptions();
     EndpointReference erf = new EndpointReference(
    "http://localhost:8088/FileUploadInfo/services/upload_service");
     options.setTo(erf);
     Object[] opAddEntryArgs = new Object[] { "E:\\WorkSpace\\.metadata\\.plugins\\org.eclipse.wst.server.core\\tmp0\\wtpwebapps\\FileUploadInfo\\WEB-INF\\classes\\upload\\20150511162101.jar" };
    // 设置返回值类型
    Class<?>[] classes = new Class<?>[] { String.class };
    // 命名空间和方法名称
    QName qname = new QName("http://upservice.fileup.com", "deleteFile");
    // 执行文件删除,接收返回值
    Object returnValue = sc.invokeBlocking(qname,
    opAddEntryArgs, classes)[0];
    System.out.println(new Date() + " 删除结束,返回值:" + returnValue);
  }
}

    UtilFile.java

package com.fileup.util;

/**

 * 基础util 类

 * @author root

 *

 */

public class UtilFile {

 /**

  * 截取文件的后缀名

  * @param fileName

  * @return

  */

    public static String fileEndName(String fileName){

     return fileName.substring(fileName.lastIndexOf("."),fileName.length() );

    }

}

你可能感兴趣的:(webservice,axis2,文件操作)