java– 文件未上传到服务器

java– 文件未上传到服务器,第1张

概述我正在从我的android应用程序上传音频文件到server.Upload代码正在我这边运行但是没有文件没有在服务器上传.这是我正在使用的代码.我还评论了代码,文件正在上传.here是代码:publicvoidUploadFile(Stringpath,Stringid){HttpURLConnectionconnection=null;Dat

我正在从我的android应用程序上传音频文件到server.Upload代码正在我这边运行但是没有文件没有在服务器上传.这是我正在使用的代码.我还评论了代码,文件正在上传.here是代码:

public voID Uploadfile(String path, String ID) {    httpURLConnection connection = null;    DataOutputStream outputStream = null;    String pathToOurfile = "" + path;    String urlServer = "http://txtAPI.com/musicApp/uploadfile.PHP?fileID=" + ID + "&sharedBy=" + sp.getString(CommonUtility.phone, CommonUtility.getPhoneNumber(ct));    String lineEnd = "\r\n";    String twoHyphens = "--";    String boundary = "*****";    int bytesRead, bytesAvailable, bufferSize;    byte[] buffer;    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=\"fileID\";filename=\"" + path + "\"" + lineEnd);        outputStream.writeBytes(lineEnd);        bytesAvailable = fileinputStream.available();        // Read file        //              bytesRead = fileinputStream.read(buffer, 0, bufferSize);//file is getting upload by this code         //              int i  =0;        //              while (bytesRead > 0)        //              {        //              i++;        //                  //              outputStream.write(buffer, 0, bufferSize);                                    //              bytesAvailable = fileinputStream.available();        //              bufferSize = Math.min(bytesAvailable, maxBufferSize);        //              bytesRead = fileinputStream.read(buffer, 0, bufferSize);        //              }        buffer = new byte[1024];        int bufferLength = 0;        while ((bufferLength = fileinputStream.read(buffer)) > 0) {            //add the data in the buffer to the file in the file output stream (the file on the sd card            Log.i("bytes available ", "" + fileinputStream.available());            Log.i("buffer length", "" + bufferLength);            outputStream.write(buffer);            //add up the size so we kNow how much is downloaded        }        outputStream.writeBytes(lineEnd);        outputStream.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);        // Responses from the server (code and message)        fileinputStream.close();        outputStream.flush();        outputStream.close();    } catch (Exception ex) {        ex.printstacktrace();        BUGSenseHandler.setLogging(true);        //Exception handling        Log.i("InsIDe catch of  ", "InsIDe catch of upload ");        BUGSenseHandler.sendException(ex);    } finally {        System.gc();    } }

解决方法:

读取的字节数应该是写入输出流的字节数,因此将其更改为以下内容:

    while ((bufferLength = fileinputStream.read(buffer)) > 0) {        //add the data in the buffer to the file in the file output stream (the file on the sd card        Log.i("bytes available ", "" + fileinputStream.available());        Log.i("buffer length", "" + bufferLength);        outputStream.write(buffer, 0, bufferLength);        //add up the size so we kNow how much is downloaded    }
总结

以上是内存溢出为你收集整理的java – 文件未上传到服务器全部内容,希望文章能够帮你解决java – 文件未上传到服务器所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1116988.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-29
下一篇 2022-05-29

发表评论

登录后才能评论

评论列表(0条)

保存