用C#写web service接收远程安卓前台图片传送然后存储在本地磁盘,打开磁盘文件能浏览传送的图片
public class MainActivity extends Activity {@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
testUpload("/mnt/sdcard/img.jpg");
}
public static byte[] readStream(InputStream inStream) throws Exception {
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
System.out.println("------>byte[] readStream(InputStream inStream)");
byte[] buffer = new byte[1024*4];
int len = -1;
while ((len = inStream.read(buffer)) != -1) {
outStream.write(buffer, 0, len);
}
outStream.close();
inStream.close();
return outStream.toByteArray();
}
//上传方法
public void testUpload(String path){
try{
File file = new File(path); //path是路径
FileInputStream fis = new FileInputStream(file);
// 对文件进行类型转换
System.out.println("FileInputStream--->currentpath"+path+"\n" );
byte[] fileStream = readStream(fis);
// 问题是现在这个 fileStream没有东西 ,出来异常了
// 08-18 02:37:07.630: WARN/System.err(1846): java.lang.RuntimeException: Cannot serialize: [B@44efa1b8
// fileStream输出的是这个---》[B@44efa1b8
System.out.println("dong+++++++++"+fileStream);
System.out.println("fileStream.length"+fileStream.length);
String SavePath ="f://picture";
String FileName = "img.jpg";
UpLoadFile(fileStream);
// String SavePath ="/mnt/sdcard/img.jpg";
// System.out.println("调用webservice 完成了 ");
// Log.i("fileStream", "start");
// File f=new File(SavePath);
// if(f.exists())
// {
// f.createNewFile();
// }
// witeStream(fileStream,f);
// fis.close();
}catch(Exception e){
e.printStackTrace();
}
}
public static boolean UpLoadFile(byte[] fileStream) throws Exception {
// boolean ret = false;
System.out.println("WebService_Login.uploadfile");
SoapObject soapObject = new SoapObject("http://, "UploadFile");
System.out.println("fileStream >>>>"+fileStream);
soapObject.addProperty("fs",fileStream); //参数1 文件字符数组
//soapObject.addProperty ("fs",new String(fileStream,"UTF-8"));
// soapObject.addProperty("SavePath", SavePath); //保存路径
// soapObject.addProperty("FileName", FileName); //参数2 文件字符串
// System.out.println("接口连接完成"+"fileStream---->"+FileName);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
SoapEnvelope.VER11);
envelope.bodyOut = soapObject;
HttpTransportSE httpTranstation = new HttpTransportSE("http://192.168.2.150/Service.asmx");
try {
httpTranstation.call(null, envelope);
Object result = envelope.getResponse();
Log.i("uploadfile", result.toString());
} catch (IOException e) {
e.printStackTrace();
} catch (XmlPullParserException e){
e.printStackTrace();
}
return false;
}
//上传文件代码
public static void witeStream(byte[] fileStream,File file) throws Exception {
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
FileOutputStream fout=new FileOutputStream(file);
System.out.println("------>byte[] readStream(InputStream inStream)");
fout.write(fileStream);
outStream.close();
}
}
以上是前台安卓端的代码,现在需要用用C#写web service接收安卓前台图片传送然后存储在本地磁盘,打开磁盘文件能浏览传送的图片,求高手看看以上代码是否正确,写写web service,要用C#写,谢谢谢!!