2011-10-11 6 views
1

を経由して私はASMX Webサービスにマルチパートの画像をアップロードするには、次のJavaコードを使用しています:アップロードマルチパート画像アンドロイド

import java.io.File; 
import org.apache.http.HttpEntity; 
import org.apache.http.HttpResponse; 
import org.apache.http.HttpVersion; 
import org.apache.http.client.HttpClient; 
import org.apache.http.client.methods.HttpPost; 
import org.apache.http.entity.mime.MultipartEntity; 
import org.apache.http.entity.mime.content.ContentBody; 
import org.apache.http.entity.mime.content.FileBody; 
import org.apache.http.impl.client.DefaultHttpClient; 
import org.apache.http.params.CoreProtocolPNames; 
import org.apache.http.util.EntityUtils; 


public class PostFile { 
    public static void main(String[] args) throws Exception { 
    HttpClient httpclient = new DefaultHttpClient(); 
    httpclient.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1); 

    HttpPost httppost = new HttpPost("http://localhost:9001/upload.asmx/SaveDocument"); 
    File file = new File("c:/TRASH/zaba_1.jpg"); 

    MultipartEntity mpEntity = new MultipartEntity(); 
    ContentBody cbFile = new FileBody(file, "image/jpeg"); 
    mpEntity.addPart("userfile", cbFile); 


    httppost.setEntity(mpEntity); 
    System.out.println("executing request " + httppost.getRequestLine()); 
    HttpResponse response = httpclient.execute(httppost); 
    HttpEntity resEntity = response.getEntity(); 

    System.out.println(response.getStatusLine()); 
    if (resEntity != null) { 
     System.out.println(EntityUtils.toString(resEntity)); 
    } 
    if (resEntity != null) { 
     resEntity.consumeContent(); 
    } 

    httpclient.getConnectionManager().shutdown(); 
    } 
} 

私の問題は、私がいないということですサーバー側で何をすべきかを知っている。 通常のファイルのアップロードに使用した次のコードは機能していないようです。

[WebMethod] 
public bool SaveDocument(Byte[] docbinaryarray, string docname) 
{ 
    string strdocPath; 
    strdocPath = "C:\\DocumentDirectory\\" + docname; 
    FileStream objfilestream =new FileStream(strdocPath,FileMode.Create,FileAccess.ReadWrite); 
    objfilestream.Write(docbinaryarray,0,docbinaryarray.Length); 
    objfilestream.Close(); 

    return true; 
} 

私はマルチパートファイルを保存するために特別な何かをする必要があり推測しているが、私は何を知りません。 何か助けていただければ幸いです。 ありがとうございました!

+0

ASMXサービスがマルチパートHTTPリクエストをサポートしているとは思いません。 –

+0

代わりにWCFサービスを選択する必要がありますか? –

答えて

1

ストリーミングを使用して終了しましたが、これはASXMでは不可能です。 代わりにWCFを使用しています。

関連する問題