Web服务器怎么接收Android发送过来的文件信息

2024-11-18 14:55:11
推荐回答(2个)
回答(1):

您好,直接用 http handler 或者 http webapi 处理 multipart-data 请求

java:

File file = new File("F:\\tmp\\taiping\\conf-1.json");

MultipartEntity mpEntity = new MultipartEntity(); // 文件传输

ContentBody cbFile = new FileBody(file);

mpEntity.addPart("fileContent", cbFile);

CloseableHttpClient client = HttpClients.createDefault();

HttpPost post = new HttpPost("http://localhost:9999/api/values");

post.setEntity(mpEntity);

try {

CloseableHttpResponse response = client.execute(post);

String result = IOUtils.toString(response.getEntity().getContent());

System.out.println(result);

} catch (Exception e) {

e.printStackTrace();

}

.net http webapi

public HttpResponseMessage Post()

{

var content = Request.Content;

var uploadDir = HttpContext.Current.Server.MapPath("~/Upload");

var newFileName = "";

var sp = new MultipartMemoryStreamProvider();

Task.Run(async () => await Request.Content.ReadAsMultipartAsync(sp)).Wait();

foreach (var item in sp.Contents)

{

if (item.Headers.ContentDisposition.FileName != null)

{

var filename = item.Headers.ContentDisposition.FileName.Replace("\"", "");

newFileName = uploadDir + "\\" + filename;

var ms = item.ReadAsStreamAsync().Result;

using (var br = new BinaryReader(ms))

{

var data = br.ReadBytes((int)ms.Length);

File.WriteAllBytes(newFileName, data);

}

}

}

var result = new Dictionary();

result.Add("result", newFileName);

var resp = Request.CreateResponse(HttpStatusCode.OK, result);

resp.Content.Headers.ContentType = new MediaTypeHeaderValue("text/plain");

return resp;

}

回答(2):

直接用 http handler 或者 http webapi 处理 multipart-data 请求

java:

File file = new File("F:\\tmp\\taiping\\conf-1.json");
MultipartEntity mpEntity = new MultipartEntity(); //

ContentBody cbFile = new FileBody(file);
mpEntity.addPart("fileContent", cbFile);

CloseableHttpClient client = HttpClients.createDefault();
HttpPost post = new HttpPost("
");
post.setEntity(mpEntity);
try {

CloseableHttpResponse response = client.execute(post);

String result = IOUtils.toString(response.getEntity().getContent());

System.out.println(result);
} catch (Exception e) {

e.printStackTrace();
}

.net http webapi

public HttpResponseMessage Post()
{
var content = Request.Content;
var uploadDir = HttpContext.Current.Server.MapPath("~/Upload");
var newFileName = "";

var sp = new MultipartMemoryStreamProvider();
Task.Run(async () => await Request.Content.ReadAsMultipartAsync(sp)).Wait();

foreach (var item in sp.Contents)
{
if (item.Headers.ContentDisposition.FileName != null)
{
var filename = item.Headers.ContentDisposition.FileName.Replace("\"", "");
newFileName = uploadDir + "\\" + filename;
var ms = item.ReadAsStreamAsync().Result;
using (var br = new BinaryReader(ms))
{
var data = br.ReadBytes((int)ms.Length);
File.WriteAllBytes(newFileName, data);
}
}
}

var result = new Dictionary();
result.Add("result", newFileName);
var resp = Request.CreateResponse(HttpStatusCode.OK, result);
resp.Content.Headers.ContentType = new MediaTypeHeaderValue("text/plain");
return resp;
}