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