English 中文(简体)
从甲 到 j鱼的装 image图
原标题:Upload image from android to java servlet and save it

我先看一看这个问题,我没有工作。

I m trying to upload an image from android app to java servlet and save it in the server. Every solution I found didn t work for me.

What my code currently does: the android app is sending the image to the servlet, when i m trying to save it the file is created, but it s empty :(

感谢你们的帮助!

我在海底客户中的代码(即文件是仪器上的存档地点):

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 Code:

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;
}
最佳回答

你错误地解释了这一问题。 图像档案不是空洞的,但图像档案是腐蚀的,因为你把整个吉大港定居地的多部分申请机构重新编为图像文件,而不是从吉大港定居方案多部分申请机构抽取含有图像的部分。

You need HttpServletRequest#getPart(,以获取多部分申请机构的部分。 如果你在Serlet 3.0(Tomcat 7, Glassfish 3,等)上重新编号,首先在rel=“nofollow noreferer”>@MultipartConfig>

@WebServlet("/GetPictureFromClient")
@MultipartConfig
public class GetPictureFromClient extends HttpServlet {
    // ...
}

之后,将doPost(>> 贴上以下文字,以便用自己的名称打碎该部分,然后将其身体作为投入流:

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

如果你仍然没有列入Serlet 3.0,那么,贪ab(。ache Commons fileUpload。 另见关于详细例子的答复:。 如何将档案上载到服务器上,使用JP/Servlet?

Oh, please get rid of the Netbeans-generated processRequest() method. It s absolutely not the right way to delegate both doGet() and doPost() to a single processRequest() method and it ll only confuse other developers and maintainers who don t use Netbeans.

问题回答

暂无回答




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...