简体   繁体   English

如何将图像从Android上传到Java

[英]How to upload image from android to java

I am developing a software in which images should be upload from android to java. 我正在开发一种应将图像从android上传到java的软件。 so far I have developed the following client on android: 到目前为止,我已经在android上开发了以下客户端:

        String url=params[0];
        String filePath=params[1];

        File file=new File(filePath);

        MultipartEntityBuilder multipartEntityBuilder=MultipartEntityBuilder.create();
        multipartEntityBuilder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
        multipartEntityBuilder.addPart("file", new FileBody(file));
        HttpPut httpPut=new HttpPut(url);
        HttpClient httpclient = new DefaultHttpClient();
        httpPut.setEntity(multipartEntityBuilder.build());
        HttpResponse response;

        try {
            response = httpclient.execute(httpPut);
            HttpEntity entity = response.getEntity();
            if (entity != null) {
                InputStream instream = entity.getContent();
                String result= convertStreamToString(instream);
                instream.close();
                return result;
            }
        } catch (Exception e) {
        }
        return null;
    }

and the server is: 服务器是:

String path=Server.imagesPath+Utilities.getRandomString(10)+".jpg";
    InputStream inputStream= arg0.getRequestBody();
    File file=new File(path);
    Files.copy(inputStream, file.toPath());
    String response="OK";
    try{
    arg0.sendResponseHeaders(200, response.length());
        OutputStream outputStream=arg0.getResponseBody();
        outputStream.write(response.getBytes());
        outputStream.close();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

The image is completely copied to the server but it is damaged and it can not be viewed. 该图像已完全复制到服务器,但已损坏,无法查看。 What is the problem? 问题是什么?

It looks as though the upload is being done as a multipart form, which allows you to insert images as part of the data being sent; 看起来好像上传是以多部分形式完成的,它允许您将图像插入为要发送的数据的一部分。 but the server is reading it as a pure octet stream rather than a multipart form. 但服务器将其读取为纯八位位组流,而不是多部分形式。

You need to choose one or the other. 您需要选择一个。 Either interpret it as form data on the server, or just send the data as a stream rather than as form data. 要么将其解释为服务器上的表单数据,要么将数据作为流而不是表单数据发送。

I'd suggest having a look at Apache Commons FileUpload , which will simplify lots of this. 我建议您看看Apache Commons FileUpload ,它将简化很多工作。

You can also use multiple data part entity for image uploading 您还可以使用多个数据部分实体进行图像上传

you can see complete demo here: http://niravranpara.blogspot.in/2012/11/upload-video-in-server.html 您可以在此处查看完整的演示: http : //niravranpara.blogspot.in/2012/11/upload-video-in-server.html

I could finally solve the problem using this article: 我最终可以使用本文解决问题:

Android Code to Upload & Download large files to server Android代码将大文件上传和下载到服务器

the code I used on the Android was: 我在Android上使用的代码是:

        String urlString=params[0];
        String path=params[1];
        File file=new File(path);
        int maxBufferSize=1024;
        URL url=null;
        try {
            url=new URL(urlString);
        } catch (MalformedURLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        HttpURLConnection connection=null;
        try {
            connection = (HttpURLConnection) url.openConnection();
        } catch (IOException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }

        connection.setDoInput(true);
        connection.setDoOutput(true);
        connection.setUseCaches(false);
        try {
            connection.setRequestMethod("PUT");
        } catch (ProtocolException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        connection.setRequestProperty("Content-Type", "image/jpg");
        OutputStream outputStream=null;
        FileInputStream fileInputStream=null;
        byte[] buffer;
        int bytesRead,bytesAvailable,bufferSize;

        try {
            outputStream=connection.getOutputStream();
            fileInputStream=new FileInputStream(file);
            bytesAvailable = fileInputStream.available();
            bufferSize = Math.min(bytesAvailable, maxBufferSize);
            buffer = new byte[bufferSize];
            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);
            }

            int serverResponseCode = connection.getResponseCode();
            String serverResponseMessage = connection.getResponseMessage();
            Log.d("test Response Code ",Integer.toString(serverResponseCode));
            Log.d("test Response Message ", serverResponseMessage);

        } catch (IOException e) {
            Log.d("test", e.getMessage());
        }

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

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