【问题标题】:Upload files from Android to server with socket使用套接字将文件从 Android 上传到服务器
【发布时间】:2011-12-01 07:31:46
【问题描述】:

我正在开发 Android 应用程序。在创建这个问题之前,我搜索了很多帖子。我想在java中使用socket从android手机上传文件。它应该在服务器端什么样的应用程序?假设用java写服务器端,应该是一个什么样的项目? 关于java应用,我只知道服务器主机——tomcat。

【问题讨论】:

    标签: java android sockets


    【解决方案1】:

    在您的情况下(因为服务器有 tomcat)如果您有服务器的 url,那么您可以使用 HttpURLConnection 将任何文件上传到服务器。并且应该在服务器端编写逻辑来接收文件 示例

    HttpURLConnection connection = null;
    DataOutputStream outputStream = null;
    DataInputStream inputStream = null;
    
    String pathToOurFile = "/sdcard/file_to_send.mp3"; //complete path of file from your android device
    String urlServer = "http://192.168.10.1/handle_upload.do";// complete path of server
    String lineEnd = "\r\n";
    String twoHyphens = "--";
    String boundary =  "*****";
    
    int bytesRead, bytesAvailable, bufferSize;
    byte[] buffer;
    int maxBufferSize = 1*1024*1024;
    
    try
    {
    FileInputStream fileInputStream = new FileInputStream(new File(pathToOurFile) );
    
    URL url = new URL(urlServer);
    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;boundary="+boundary);
    
    outputStream = new DataOutputStream( connection.getOutputStream() );
    outputStream.writeBytes(twoHyphens + boundary + lineEnd);
    outputStream.writeBytes("Content-Disposition: form-data; name=\"uploadedfile\";filename=\"" + pathToOurFile +"\"" + lineEnd);
    outputStream.writeBytes(lineEnd);
    
    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);
    }
    
    outputStream.writeBytes(lineEnd);
    outputStream.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);
    
    // Responses from the server (code and message)
    serverResponseCode = connection.getResponseCode();
    serverResponseMessage = connection.getResponseMessage();
    
    fileInputStream.close();
    outputStream.flush();
    outputStream.close();
    }
    catch (Exception ex)
    {
    //Exception handling
    }
    

    【讨论】:

    • +1 很棒的帖子,也许对我期望服务器端匹配哪种代码的简要总结对我有用。
    • Sunil,您的解决方案是否意味着我需要创建一个网络服务或网站来接收文件?有没有办法在服务器端没有代码的情况下从客户端获取文件?喜欢 FTP?
    • 检查 SPK 的 FTP 上传链接。它链接到一个预制的 FTP 类来为您上传。但请记住,使用标准 FTP 上传可能不安全(因为任何人都可以窃取登录数据并滥用服务器!)。
    猜你喜欢
    • 1970-01-01
    • 2014-01-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-21
    • 1970-01-01
    • 2018-01-13
    • 2012-04-06
    相关资源
    最近更新 更多