在Android上传到.NET WebService
-
21-12-2019 - |
题
我一直在使用这个在服务器上上传映像的Android应用程序。我能够使用PHP上传成功上传,但我无法在.NET WebService上载入。负责WebService的人给了我的代码,所以我可以看看它。
这里是。
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中没有经验,所以我不确定如何处理这种情况。似乎在WebService中的上传图像功能使用Filestream,因为您可以从上面的参数中看到。
编辑
这是我的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();
}
. 解决方案
我发现了一个教程我在上传文件时没有成功,但这是另一个问题。
不隶属于 StackOverflow