Я работаю с этим приложением для Android, которое загружает изображение на сервер. Мне удалось успешно загрузить с помощью PHP, но у меня возникли проблемы с загрузкой на веб-сервис .net. Ребята, отвечающие за веб-сервис, дали мне код, чтобы я мог его посмотреть.
Вот.
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, поэтому я не знаю, как справиться с этой ситуацией. Кажется, что функция загрузки изображения в веб-сервисе использует файловый поток, как вы можете видеть из приведенных выше параметров.
ИЗМЕНИТЬ
Вот мой 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();
}