简体   繁体   English

图像上传到Android中的服务器

[英]Image uploading to server in android

I had some problem before which I solved. 我有一些问题需要解决。 Thanks a lot to everyone who replied to the queries. 非常感谢所有回答查询的人。

I am trying to upload image from my android application to server (Servelet). 我正在尝试将图像从我的android应用程序上传到服务器(Servelet)。 I have also emulated the SD Card which contains one jpeg image. 我还模拟了包含一张jpeg图像的SD卡。

I have been able to make connection with the server and get some message back. 我已经能够与服务器建立连接并获得一些消息。 But so far I have not been able to retrieve the image on the server. 但是到目前为止,我还无法在服务器上检索图像。 I get error msg on the server. 我在服务器上收到错误消息。 I am using multipart option. 我正在使用分段选项。 The code are as follows. 代码如下。 Plz have a look and let me know what wrong I am doing. 请看看,让我知道我在做什么错。

ANDROID SIDE 安卓侧

     String path="http://10.0.2.2:8080/ImageLocalizer/Localize";
        String pathToFile="/sdcard/building.jpg";
        text1.setText(path);

        int bytesRead, bytesAvailable, bufferSize;
        byte[] buffer;
        int maxBufferSize = 1*1024*1024;
        try
        {
        FileInputStream fileInputStream = new FileInputStream(new File(pathToFile) );

        URL url = new URL(path);
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();

        // Allow Inputs & Outputs
        connection.setDoInput(true);
        connection.setDoOutput(true);
        connection.setUseCaches(false);

        // Enable POST method
        connection.setRequestMethod("POST");

        connection.setRequestProperty("Connection", "Keep-Alive");
        connection.setRequestProperty("Content-Type",  "multipart/form-data");
        connection.setRequestProperty("FileName", "building.jpg");

        DataOutputStream outputStream =new DataOutputStream( connection.getOutputStream());
        bytesAvailable = fileInputStream.available();
        bufferSize = Math.min(bytesAvailable, maxBufferSize);
        buffer = new byte[bufferSize];

        // Read file
        bytesRead = fileInputStream.read(buffer, 0, bufferSize);

        while (bytesRead > 0)
        {
            outputStream.write(buffer, 0, bufferSize);
            bytesAvailable = fileInputStream.available();
            bufferSize = Math.min(bytesAvailable, maxBufferSize);
            bytesRead = fileInputStream.read(buffer, 0, bufferSize);
        }

        fileInputStream.close();
        outputStream.flush();
        outputStream.close();
        text1.setText(path);

        BufferedReader in =new BufferedReader(new InputStreamReader( connection.getInputStream() ) );

        String response="",msg="";
        while ( (response = in.readLine()) != null ) {
            msg+=response;
        }
        text2.setText(msg);
    }        
    catch (Exception ex)
     {}

    }

SERVELET SIDE 服务端

   doPost()
    {
            System.out.println("RUNNING");
             InputStream in = request.getInputStream();
                BufferedReader r = new BufferedReader(new InputStreamReader(in));
                StringBuffer buf = new StringBuffer();
                String line;

                PrintWriter out = response.getWriter();
                out.println("Image copied !!");
                out.close();
                try  
                {  
                        PrintWriter out1=response.getWriter();  
                        FileItemFactory factory = new DiskFileItemFactory();  
                        ServletFileUpload upload = new ServletFileUpload(factory);  
                        String s[]=new String[30];  
                        byte i=0;  
                        boolean isMultipart = ServletFileUpload.isMultipartContent(request);  
                        if(isMultipart)  
                        {  
                            List items=upload.parseRequest(request);      
                            Iterator iterator=items.iterator();  
                            while(iterator.hasNext())  
                            {  
                                FileItem fitem=(FileItem)iterator.next();  
                                if(!fitem.isFormField())  
                                {  
                                    StringBuffer s2=new StringBuffer(fitem.getName());                    
                                    if(s2!=null && s2.length()>0)  
                                    {  
                                        File fl=new File("H:\\abcd.jpg");  
                                        fitem.write(fl);  
                                    }  
                                }  
                                if(fitem.isFormField())  
                                {  
                                    s[i]=fitem.getString();  
                                    i++;  
                                }  
                            }  

                        }  

                }catch(Exception e){e.printStackTrace();}  


    }

I dont think the body of your request is formatted corrected as multi part form data. 我不认为您的要求的正文已格式化为多部分表格数据。 The body should contain several different delimited sections, with each section containing the filename and encoding of that section. 主体应包含几个不同的定界部分,每个部分均包含该部分的文件名和编码。 See the following URL for an example: 有关示例,请参见以下URL:

http://chxo.com/be2/20050724_93bf.html http://chxo.com/be2/20050724_93bf.html

If you're only posting one file at a time I would remove the multi part form data header and just post the contents of the file as the request body. 如果您一次只发布一个文件,那么我将删除多部分表单数据标题,而仅将文件内容作为请求正文发布。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM