gpt4 book ai didi

android - 通过android将多部分图像上传到asmx web服务

转载 作者:行者123 更新时间:2023-11-30 04:30:52 26 4
gpt4 key购买 nike

我正在使用以下 java 代码将多部分图像上传到 ASMX 网络服务:

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;
}

我猜我需要做一些特别的事情来保存多部分文件,但我不知道是什么。任何帮助表示赞赏。谢谢!

最佳答案

我最终使用了流式传输,这在 AS 中是不可能的。我改用 WCF。

关于android - 通过android将多部分图像上传到asmx web服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7724464/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com