上传和2个或多个文件从服务器下载到/ [英] Upload and download 2 or more files to/from server

查看:264
本文介绍了上传和2个或多个文件从服务器下载到/的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使可Android应用程序连接到服务器的应用程序,我需要上传和下载2个或多个文件
我发现这个code

I am trying to make an app that can connect android app to the server, and I need to upload and download 2 or more files I found this code

private void doFileUpload() {
        HttpURLConnection conn = null;
        DataOutputStream dos = null;
        DataInputStream inStream = null;
        String existingFileName = Environment.getExternalStorageDirectory()
                .getAbsolutePath() + "/android/data/[package]/files/productHistory";
        **String existingFileName2 = Environment.getExternalStorageDirectory()
                .getAbsolutePath() + "/android/data/[package]/files/productStock";**
        String lineEnd = "\r\n";
        String twoHyphens = "--";
        String boundary = "*****";
        int bytesRead, bytesAvailable, bufferSize;
        byte[] buffer;
        int maxBufferSize = 1 * 1024 * 1024;
        String responseFromServer = "";
        String urlString = "http://192.168.1.112/johnson/learn/android/";
        try {
            // ------------------ CLIENT REQUEST
            FileInputStream fileInputStream = new FileInputStream(new File(
                    existingFileName));
            // open a URL connection to the Servlet
            URL url = new URL(urlString);
            // Open a HTTP connection to the URL
            conn = (HttpURLConnection) url.openConnection();
            // Allow Inputs
            conn.setDoInput(true);
            // Allow Outputs
            conn.setDoOutput(true);
            // Don't use a cached copy.
            conn.setUseCaches(false);
            // Use a post method.
            conn.setRequestMethod("POST");
            conn.setRequestProperty("Connection", "Keep-Alive");
            conn.setRequestProperty("Content-Type",
                    "multipart/form-data;boundary=" + boundary);
            dos = new DataOutputStream(conn.getOutputStream());
            dos.writeBytes(twoHyphens + boundary + lineEnd);
            dos.writeBytes("Content-Disposition: form-data; name=\"uploadedfile\";filename=\""
                    + existingFileName + "\"" + lineEnd);
            **dos.writeBytes("Content-Disposition: form-data; name=\"uploadedfile\";filename=\""
                    + existingFileName2 + "\"" + lineEnd);**
            dos.writeBytes(lineEnd);
            // create a buffer of maximum size
            bytesAvailable = fileInputStream.available();
            bufferSize = Math.min(bytesAvailable, maxBufferSize);
            buffer = new byte[bufferSize];
            // read file and write it into form...
            bytesRead = fileInputStream.read(buffer, 0, bufferSize);
            while (bytesRead > 0) {
                dos.write(buffer, 0, bufferSize);
                bytesAvailable = fileInputStream.available();
                bufferSize = Math.min(bytesAvailable, maxBufferSize);
                bytesRead = fileInputStream.read(buffer, 0, bufferSize);
            }
            // send multipart form data necesssary after file data...
            dos.writeBytes(lineEnd);
            dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);
            // close streams
            Log.e("Debug", "File is written");
            fileInputStream.close();
            dos.flush();
            dos.close();
        } catch (MalformedURLException ex) {
            Log.e("Debug", "error: " + ex.getMessage(), ex);
        } catch (IOException ioe) {
            Log.e("Debug", "error: " + ioe.getMessage(), ioe);
        }
        // ------------------ read the SERVER RESPONSE
        try {
            inStream = new DataInputStream(conn.getInputStream());
            String str;

            while ((str = inStream.readLine()) != null) {
                Log.e("Debug", "Server Response " + str);
            }
            inStream.close();

        } catch (IOException ioex) {
            Log.e("Debug", "error: " + ioex.getMessage(), ioex);
        }
    }

大胆的文字被编辑的我,
但它失败了,它只能上传第一个

The bold text is edited by me, but it is fail, it only upload the first one

我的问题是1如何2 /多个文件上传与code?
2.以及如何下载呢?

my question is 1. how to upload 2/more files with the code? 2. and how to download them?

在此先感谢

推荐答案

不知道,如果你要使用特定的服务器,但解析提供了一个甜蜜的SDK和直观的API发送/接收数据(在你的情况下图像),并从服务器。我建议你​​尝试了这一点,如果你不想花太多的时间写作code发送和接收的数据和从服务器。 (没有,我没有在分析工作,他们的工具只是一个大风扇:P)。

Not sure if you have to use a specific server, yet Parse offers a sweet SDK with intuitive API to send/receive data (images in you case) to and from the server. I'd suggest you try that out if you don't want to spend to much time writing code that sends and receives data to and from a server. (and no, I don't work at Parse, just a big fan of their tools :P).

这篇关于上传和2个或多个文件从服务器下载到/的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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