이미지 업로드 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;
}
.
문제는이 상황을 처리하는 방법을 확실하지 않아야합니다.위의 매개 변수에서 볼 수 있듯이 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