using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Drawing;
using System.IO;
/// <summary>
///UploadData 的摘要说明
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
//若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消对下行的注释。
// [System.Web.Script.Services.ScriptService]
public class UploadData : System.Web.Services.WebService {
public UploadData () {
//如果使用设计的组件,请取消注释以下行
//InitializeComponent();
}
[WebMethod]
public string UploadImg(string bytestr)
{
string name = "";
string mess = "";
try
{
name = DateTime.Now.Year.ToString() + DateTime.Now.Month + DateTime.Now.Day + DateTime.Now.Hour + DateTime.Now.Minute + DateTime.Now.Second;
if (Directory.Exists(Server.MapPath("image\\")) == false)//如果不存在就创建file文件夹
{
Directory.CreateDirectory(Server.MapPath("image\\"));
}
bool flag = StringToFile(bytestr, Server.MapPath("image\\") + "" + name + ".jpg");
string filePath = "/image/" + name + ".jpg";
}
catch (Exception ex)
{
mess = ex.Message;
}
if (mess != "")
{
return mess;
}
else
{
return "文件上传成功";
}
}
protected System.Drawing.Image Base64StringToImage(string strbase64)
{
try
{
byte[] arr = Convert.FromBase64String(strbase64);
MemoryStream ms = new MemoryStream(arr);
//Bitmap bmp = new Bitmap(ms);
ms.Write(arr, 0, arr.Length);
System.Drawing.Image image = System.Drawing.Image.FromStream(ms);
ms.Close();
return image;
//return bmp;
}
catch (Exception ex)
{
throw ex;
}
}
/// <summary>
/// 把经过base64编码的字符串保存为文件
/// </summary>
/// <param name="base64String">经base64加码后的字符串 </param>
/// <param name="fileName">保存文件的路径和文件名 </param>
/// <returns>保存文件是否成功 </returns>
public static bool StringToFile(string base64String, string fileName)
{
//string path = Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetName().CodeBase) + @"/beapp/" + fileName;
System.IO.FileStream fs = new System.IO.FileStream(fileName, System.IO.FileMode.Create);
System.IO.BinaryWriter bw = new System.IO.BinaryWriter(fs);
if (!string.IsNullOrEmpty(base64String) && File.Exists(fileName))
{
bw.Write(Convert.FromBase64String(base64String));
}
bw.Close();
fs.Close();
return true;
}
}
package com.finly.test;
import static org.junit.Assert.*;
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Hashtable;
import org.json.JSONException;
import org.json.JSONObject;
import org.junit.Test;
import org.kobjects.base64.Base64;
import com.finly.util.VisitWebServiceHelp;
public class AndroidTest7 {
@Test
public void test() throws JSONException, IOException {
String namespace = "http://tempuri.org/";
String url = "http://192.168.1.186:8082/Service/UploadData.asmx";
//String url ="https://register.gs-markets.com/webservice/Service/Customerwebservice.asmx";
String way="UploadImg";
VisitWebServiceHelp Vh=new VisitWebServiceHelp();
Hashtable<String, String> ht =ht3();
String res = Vh.VisitWebService(namespace,url,way,ht);
System.out.println(res);
}
private Hashtable<String, String> ht3()throws JSONException, IOException{
String fileName="C:\\Users\\User\\Desktop\\fff.png";
FileInputStream fis = new FileInputStream(fileName);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] buffer = new byte[8192];
int count = 0;
while((count = fis.read(buffer)) >= 0){
baos.write(buffer, 0, count);
}
String uploadBuffer = new String(Base64.encode(baos.toByteArray()));
//JSONObject PriceNotice =new JSONObject();
//User.put("Phone", "234");
Hashtable<String, String> ht = new Hashtable<String, String>();
ht.put("bytestr",uploadBuffer);
System.out.println(ht);
return ht;
}
}