在Android中通过XFire WebService远程上传以及下载图片
fmms
12年前
有时我们可以看到,图片往往被提交到服务器端,这里我通过XFire框架来实现远程上传以及下载图片的功能。
首先我们来看看从服务器端下载图片的功能:
我用的是KSOAP框架,我之前的文章有讲述过。在这里不要多说,贴上代码。
首先我们看看Acitivity中的代码:
package com.net.connect.app; import java.io.File; import java.io.FileInputStream; import android.app.Activity; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.ImageView; import android.widget.Toast; import com.base.encry.decry.app.Base64; import com.base.file.util.FileOperate; import com.cn.blogs.ksoap.app.MyWebServiceHelper; public class UpAndDownPicFileActivity extends Activity { ImageView imageView; public static final String filename = "xfire.png"; public static final String fileDir = "/sdcard/xmlfile/"; public MyWebServiceHelper myWebServiceHelper = new MyWebServiceHelper(); @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.upanddownpic); imageView = (ImageView) this.findViewById(R.id.imageView1); // Bitmap // bitmap=BitmapFactory.decodeFile("/sdcard/xmlfile/"+"xfire.png"); // imageView.setImageBitmap(bitmap); Button downButton = (Button) findViewById(R.id.downbutton); downButton.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub myWebServiceHelper.getTranferFileString(filename); // 收到服务器端发送过来的图片,然后显现在图片框中 Bitmap bitmap = BitmapFactory.decodeFile(fileDir + filename); imageView.setImageBitmap(bitmap); } }); Button upButton = (Button) findViewById(R.id.upbutton); upButton.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub String fileContent = ""; FileOperate.OpenOrCreateFile(filename); fileContent = FileOperate.bin2XmlString(fileDir+filename); // Toast.makeText(UpAndDownPicFileActivity.this, fileContent, Toast.LENGTH_LONG).show(); String result=myWebServiceHelper.upToServerOfFileStringWithEncode(filename, fileContent); try { Thread.currentThread().sleep(2000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } Toast.makeText(UpAndDownPicFileActivity.this, result, Toast.LENGTH_LONG).show(); // 收到服务器端发送过来的图片,然后显现在图片框中 //imageView.setImageBitmap(null); } }); } }
分别实现了远程上传和下载图片的功能。它的处理类集中在MyWebServiceHelper,它实现了远程获取和上传的细节功能。
这个功能都是通过KSOAP框架实现的。下面贴上它的代码,如下所示:
// WSDL文档中的命名空间 private static final String targetNameSpace = "http://android.googlepages.com/"; // WSDL文档中的URL private static final String WSDL = "http://10.10.178.71:8888/WSDLApp/services/MyService"; // 需要调用的方法名(获得Myervices中的helloWorld方法) private static final String getHelloworld = "helloWorld"; //需要调用的方法名(获得Myervices中的login方法) private static final String getLogin="login"; //获取加密图片的字符串 private static final String getTranferFileStringWithEncode="tranferFileStringWithEncode"; //获取加密图片的字符串 private static final String upToServerOfFileContent="fetchFileStringWithEncode"; public String getTranferFileString(String filename) { String fileContent = ""; SoapObject soapObject = new SoapObject(targetNameSpace,getTranferFileStringWithEncode); SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11); envelope.dotNet = false; envelope.setOutputSoapObject(soapObject); HttpTransportSE httpTranstation=new HttpTransportSE(WSDL); try { httpTranstation.call(targetNameSpace + getTranferFileStringWithEncode, envelope); SoapObject result = (SoapObject ) envelope.bodyIn;//getResponse(); fileContent=(String) result.getProperty(0).toString(); //String strFile="downfromserive"+Math.random()+".png"; FileOperate.OpenOrCreateFile(filename); FileOperate.xmlString2Bin(fileContent, new File(filename)); // 也可以通过下面方式获得str // SoapPrimitive result = (SoapPrimitive ) envelope.getResponse(); // str=result.toString(); // 直指value字符串值 } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (XmlPullParserException e) { // TODO Auto-generated catch block e.printStackTrace(); } return fileContent; } public String upToServerOfFileStringWithEncode(String filename,String fileContent) { String str = ""; SoapObject soapObject = new SoapObject(targetNameSpace,upToServerOfFileContent); soapObject.addProperty("filename", filename); soapObject.addProperty("fileContent", fileContent); SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11); envelope.dotNet = false; envelope.setOutputSoapObject(soapObject); HttpTransportSE httpTranstation=new HttpTransportSE(WSDL); try { httpTranstation.call(targetNameSpace + upToServerOfFileContent, envelope); SoapObject result = (SoapObject ) envelope.bodyIn;//getResponse(); str=(String) result.getProperty(0).toString(); //返回上传成功0,1标志位 } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); return e.getMessage(); } catch (XmlPullParserException e) { // TODO Auto-generated catch block e.printStackTrace(); return e.getMessage(); } return str; }
在第二个函数中有:
SoapObject soapObject = new SoapObject(targetNameSpace,upToServerOfFileContent); soapObject.addProperty("filename", filename); soapObject.addProperty("fileContent", fileContent);
这个是android客户端传送服务器端的参数参数。
而里面有个FileOperate.java类,这个类负责对文件操作。我封装在这个类中,方便集中处理以及调用。
下面贴上代码。如下所示:
package com.base.file.util; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import com.base.encry.decry.app.Base64; public class FileOperate { //在SD卡中创建文件 public static void OpenOrCreateFile(String filename){ // 获取扩展SD卡设备状态 String sDStateString = android.os.Environment .getExternalStorageState(); if (sDStateString.equals(android.os.Environment.MEDIA_MOUNTED)) { // String strFile="downfromserive"+Math.random()+".png"; // 获取扩展存储设备的文件目录 File SDFile = android.os.Environment .getExternalStorageDirectory(); File destDir = new File("/sdcard/xmlfile"); // File destDir = new File(SDFile.getAbsolutePath() + // destDirStr); if (!destDir.exists()) destDir.mkdir(); // Toast.makeText(SDCardTest., text, duration) // 打开文件 File myFile = new File(destDir + File.separator + filename); // 判断是否存在,不存在则创建 if (!myFile.exists()) { try { myFile.createNewFile(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } } //删除指定文件,比如临时文件 public static void removeFile(String filename){ if(new File(filename).exists()) new File(filename).delete(); } //解密,并将内容写入至指定文件中 public static boolean xmlString2Bin(String base64String, File file) { byte[] data; FileOutputStream output = null; boolean ret = false; try { data = Base64.decode(base64String); output = new FileOutputStream(file); output.write(data); output.close(); ret = true; } catch (Exception e) { e.printStackTrace(); } return ret; } //将文件内容加密 public static String bin2XmlString(String filename) { byte[] data = null; FileInputStream input = null; String ret = null; int n; try { data = new byte[(int) new File(filename).length()]; input = new FileInputStream(new File(filename)); n = input.read(data);//这个就是一个文件读取过程。没有写while,一次性读完 input.close(); ret = new String(Base64.encode(data)); } catch (Exception e) { e.printStackTrace(); } return ret; } }
当然,我们还在看看这个Activity.java中的布局文件。
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" /> <Button android:id="@+id/downbutton" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="下载" /> <Button android:id="@+id/upbutton" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="上传" /> <ImageView android:id="@+id/imageView1" android:layout_width="fill_parent" android:layout_height="wrap_content" /> </LinearLayout>
而服务器端用的是XFire框架,这在之前的文章中讲过。不必多说。
看下面的代码:
在AccountImp.java中实现IAccount.java方法。
public String tranferFileStringWithEncode() { // TODO Auto-generated method stub return FileOperate.bin2XmlString("E://Workspaces//workspace//WSDLApp//"+"girl.png"); } public String fetchFileStringWithEncode(String filename, String fileContent) { // TODO Auto-generated method stub //创建该新文件,并返回成功 try { //打开并创建文件 FileOperate.OpenOrCreateFile(filename); String pathFile="E://Workspaces//workspace//WSDLApp"; //解密,并将内容添加至该文件中 FileOperate.xmlString2Bin(fileContent, new File("E://Workspaces//workspace//WSDLApp//xfire.png")); return "上传成功"; } catch (Exception e) { // TODO: handle exception return "上传失败"; } }
IAccount.java
//将 POJO 发布成 Web 服务:有两种方法,接口和实现类是其中之一 public interface IAccount { public int account(int x,int y); public String helloWorld(String str); //访问mysql数据库 public int login(String username,String password); //传送图片字符串 public String tranferFileStringWithEncode(); //接收远程传送过来的图片字符串 public String fetchFileStringWithEncode(String username,String filename); }
而这个XFIRE要在web.xml中注册.
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.4" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> <servlet> <servlet-name>XFireServlet</servlet-name> <servlet-class>org.codehaus.xfire.transport.http.XFireConfigurableServlet</servlet-class> <load-on-startup>0</load-on-startup> </servlet> <servlet-mapping> <servlet-name>XFireServlet</servlet-name> <url-pattern>/services/*</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>
这样Web服务就用去查找services目录下。所以还要在WebServices目录下(在该服务器端要目录下)创建
services.xml文件。如下所示:
<!-- 服务一:简单计算,直接输出 --> <service> <name>MyService</name> <serviceClass>test.IAccount</serviceClass> <implementationClass>test.AccountImp</implementationClass> <mce:style><!-- wrapped --></mce:style><style mce_bogus="1">wrapped</style> <use>literal</use> <scope>application</scope> <namespace>http://android.googlepages.com/</namespace> </service>
最终实现效果如下所示:
点击上传按钮
点击下载按钮
来自:http://blog.csdn.net/haha_mingg/article/details/6347588