Android中的图像上传到.NETWebService
我一直在使用这个Android应用程序在服务器上上传图像。我可以使用PHP成功上传,但在.NETWebService上上传时遇到了问题。负责网络服务的人把代码给了我,这样我就可以看一看了 给你Android中的图像上传到.NETWebService,android,web-services,androidhttpclient,android-webservice,Android,Web Services,Androidhttpclient,Android Webservice,我一直在使用这个Android应用程序在服务器上上传图像。我可以使用PHP成功上传,但在.NETWebService上上传时遇到了问题。负责网络服务的人把代码给了我,这样我就可以看一看了 给你 public Stream FileUpload(string fileName, Stream fileStream) { var serverPath = System.Web.Hosting.HostingEnvironment.MapPath("~/File
public Stream FileUpload(string fileName, Stream fileStream)
{
var serverPath = System.Web.Hosting.HostingEnvironment.MapPath("~/FileUpload/");
if (File.Exists(serverPath + fileName)) File.Delete(serverPath + fileName); // delete file if already used
//FileStream fileToupload = new FileStream("D:\\FileUpload\\" + fileName, FileMode.Create);
FileStream fileToupload = new FileStream(serverPath + fileName, FileMode.Create);
byte[] bytearray = new byte[10000];//
int bytesRead, totalBytesRead = 0;
do
{
bytesRead = fileStream.Read(bytearray, 0, bytearray.Length);
totalBytesRead += bytesRead;
} while (bytesRead > 0);
fileToupload.Write(bytearray, 0, bytearray.Length);
fileToupload.Close();
fileToupload.Dispose();
FileStream fs = File.OpenRead(serverPath + fileName);
WebOperationContext.Current.OutgoingResponse.ContentType = "image/jpeg";
return fs;
}
问题是我在.net方面没有经验,所以我不确定如何处理这种情况。从上面的参数可以看出,Web服务中的upload image函数似乎使用了文件流
编辑
这是我的java代码
HttpResponse httpResponse = null;
InputStream inputStream;
try {
inputStream = new FileInputStream(new File(filePath));
byte[] data;
try {
data = IOUtils.toByteArray(inputStream);
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(
"http://localhost/fileUpload");
InputStreamBody inputStreamBody = new InputStreamBody(
new ByteArrayInputStream(data), fileName);
MultipartEntityBuilder multipartEntity = MultipartEntityBuilder
.create();
multipartEntity.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
multipartEntity.addPart("file", inputStreamBody);
HttpEntity entity = multipartEntity.build();
httpPost.setEntity(entity);
httpResponse = httpClient.execute(httpPost);
if (httpResponse != null) {
} else {
}
} catch (IOException e) {
e.printStackTrace();
}
} catch (FileNotFoundException e1) {
e1.printStackTrace();
}
我找到了一本教程。我未能成功上传文件,但这是另一个问题。您得到了什么回应?