servlet android,将图像从android上传到Java servlet并保存

我一直在寻找这个东西,对我没有任何帮助。

我正在尝试将图像从android应用程序上传到java servlet并将其保存在服务器中。

我发现的每个解决方案都不适合我。

我的代码当前正在执行的操作:android应用正在将图像发送到servlet,

当我尝试保存它时,文件已创建,但是它是空的:(

谢谢你的帮助!

我在android客户端中的代码(i_file是设备上的文件位置):

public static void uploadPictureToServer(String i_file) throws ClientProtocolException, IOException {

// TODO Auto-generated method stub

HttpClient httpclient = new DefaultHttpClient();

httpclient.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);

HttpPost httppost = new HttpPost("http://192.168.1.106:8084/Android_Server/GetPictureFromClient");

File file = new File(i_file);

MultipartEntity mpEntity = new MultipartEntity();

ContentBody cbFile = new FileBody(file,"image/jpeg");

mpEntity.addPart("userfile", cbFile);

httppost.setEntity(mpEntity);

System.out.println("executing request" + httppost.getRequestLine());

HttpResponse response = httpclient.execute(httppost);

HttpEntity resEntity = response.getEntity();

System.out.println(response.getStatusLine());

if (resEntity != null) {

System.out.println(EntityUtils.toString(resEntity));

}

if (resEntity != null) {

resEntity.consumeContent();

}

httpclient.getConnectionManager().shutdown();

}

我在服务器端的代码:

protected void doPost(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

processRequest(request, response);

InputStream in = request.getInputStream();

OutputStream out = new FileOutputStream("C:\\myfile.jpg");

IOUtils.copy(in, out); //The function is below

out.flush();

out.close();

}

IOUtils.copy代码:

public static long copy(InputStream input, OutputStream output) throws IOException {

byte[] buffer = new byte[4096];

long count = 0L;

int n = 0;

while (-1 != (n = input.read(buffer))) {

output.write(buffer, 0, n);

count += n;

}

return count;

}

这不仅与客户端有关,还必须在服务器(servlet)上实现它。 顺便说一句:processRequest是做什么的?

感谢您的答复。 我阅读了链接中的信息,但似乎无法找出解决问题的方法。

您的Servlet代码看起来如何? processRequest中会发生什么?

processRequest()什么也没做,所以我删除了它。 在这里得到答案。 感谢您的@home帮助。

您误解了问题。 图像文件不是空的,但是图像文件已损坏,因为您将整个HTTP多部分请求正文存储为图像文件,而不是从HTTP多部分请求正文中提取包含图像的部分。

您需要HttpServletRequest#getPart()以获得多部分请求正文的部分。 如果您已经在使用Servlet 3.0(Tomcat 7,Glassfish 3等),请首先使用@MultipartConfig注释servlet。

@WebServlet("/GetPictureFromClient")

@MultipartConfig

public class GetPictureFromClient extends HttpServlet {

// ...

}

然后按如下所示修复您的doPost(),以按名称命名该零件,然后将其主体作为输入流:

InputStream in = request.getPart("userfile").getInputStream();

// ...

如果您仍未使用Servlet 3.0,请使用Apache Commons FileUpload。 另请参见此答案以获取详细示例:如何使用JSP / Servlet将文件上传到服务器?

哦,请摆脱Netbeans生成的processRequest()方法。 将doGet()和doPost()都委派给单个processRequest()方法绝对不是正确的方法,并且只会使其他不使用Netbeans的开发人员和维护人员感到困惑。

非常感谢! 它终于工作了...现在我可以带着微笑去健身房了! 附言:我摆脱了processRequest()。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值