图片上传到Android中的服务器 [英] Image uploading to server in android

查看:162
本文介绍了图片上传到Android中的服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我解决了之前的一些问题。非常感谢大家谁回复了查询。

我想从我的Andr​​oid应用程序上传图片到服务器(Servelet的)。我也效仿其中包含了一个JPEG图像的SD卡。

我已经能够做出与服务器的连接并得到一些消息发回。但到目前为止,我还没有能够检索服务器上的图像。我得到的服务器上的错误味精。我使用多选项。在code是如下。 PLZ一看,让我知道我做错了什么。

ANDROID SIDE

 字符串路径=htt​​p://10.0.2.2:8080/ImageLocalizer/Localize;
        字符串pathToFile =/ SD卡/ building.jpg
        text1.setText(路径);        INT读取动作,方bytesAvailable,缓冲区大小;
        字节[]缓冲区;
        INT MAXBUFFERSIZE = 1 * 1024 * 1024;
        尝试
        {
        的FileInputStream的FileInputStream =新的FileInputStream(新文件(pathToFile));        网址URL =新的URL(路径);
        HttpURLConnection的连接=(HttpURLConnection类)url.openConnection();        //允许输入和放大器;输出
        connection.setDoInput(真);
        connection.setDoOutput(真);
        connection.setUseCaches(假);        //启用POST方法
        connection.setRequestMethod(POST);        connection.setRequestProperty(连接,保持活动);
        connection.setRequestProperty(内容类型,多部分/表单数据);
        connection.setRequestProperty(文件名,building.jpg);        DataOutputStream类的OutputStream =新的DataOutputStream类(connection.getOutputStream());
        参考bytesAvailable = fileInputStream.available();
        BUFFERSIZE = Math.min(方bytesAvailable,MAXBUFFERSIZE);
        缓冲区=新的字节[缓冲区大小]        //读取文件
        读取动作= fileInputStream.read(缓冲液,0,缓冲区大小);        而(读取动作大于0)
        {
            outputStream.write(缓冲液,0,缓冲区大小);
            参考bytesAvailable = fileInputStream.available();
            BUFFERSIZE = Math.min(方bytesAvailable,MAXBUFFERSIZE);
            读取动作= fileInputStream.read(缓冲液,0,缓冲区大小);
        }        fileInputStream.close();
        outputStream.flush();
        outputStream.close();
        text1.setText(路径);        在的BufferedReader =新的BufferedReader(新的InputStreamReader(connection.getInputStream()));        字符串响应=,味精=;
        而((响应= in.readLine())!= NULL){
            味精+ =响应;
        }
        text2.setText(MSG);
    }
    赶上(异常前)
     {}    }

Servelet的SIDE

 的doPost()
    {
            的System.out.println(RUNNING);
             InputStream的时间= request.getInputStream();
                BufferedReader中R =新的BufferedReader(新的InputStreamReader(中));
                StringBuffer的BUF =新的StringBuffer();
                串线;                PrintWriter的OUT = response.getWriter();
                通过out.println(图像复制!);
                out.close();
                尝试
                {
                        PrintWriter的OUT1 = response.getWriter();
                        FileItemFactory厂=新DiskFileItemFactory();
                        ServletFileUpload上传=新ServletFileUpload(工厂);
                        一个String [] =新的String [30];
                        字节I = 0;
                        布尔isMultipart = ServletFileUpload.isMultipartContent(请求);
                        如果(isMultipart)
                        {
                            列表项= upload.parseRequest(请求);
                            迭代器迭代器= items.iterator();
                            而(iterator.hasNext())
                            {
                                的FileItem fitem =(的FileItem)iterator.next();
                                如果(!fitem.isFormField())
                                {
                                    StringBuffer的S2 =新的StringBuffer(fitem.getName());
                                    如果(S2 =空&放大器;!&放大器; s2.length()大于0)
                                    {
                                        文件F1 =新的文件(H:\\\\ abcd.jpg);
                                        fitem.write(佛罗里达州);
                                    }
                                }
                                如果(fitem.isFormField())
                                {
                                    S [i] = fitem.getString();
                                    我++;
                                }
                            }                        }                }赶上(例外五){e.printStackTrace();}
    }


解决方案

我不认为你的请求体格式修正为多部分的表单数据。主体应该包含几个不同的分隔段,包含该部分的文件名和编码每个部分。请参阅以下网址中的示例:

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

如果你一次只能发布一个文件,我会删除多部分表单数据头和刚刚发布的文件作为请求正文的内容。

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). I have also emulated the SD Card which contains one jpeg image.

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:

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.

这篇关于图片上传到Android中的服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆