android图片上传,文件在服务器中损坏

android图片上传,文件在服务器中损坏,第1张

概述我正在做一个项目,我必须使用URL将 Image上传到服务器. 但是当我上传文件时,文件在服务器中已损坏,当我打开文件时,它显示错误消息“文件已损坏”.我怎样才能克服这个问题. 这是我的代码: String lineEnd = "\r\n"; String twoHyphens = "--"; String boundary = "*****"; 我正在做一个项目,我必须使用URL将 Image上传到服务器.
但是当我上传文件时,文件在服务器中已损坏,当我打开文件时,它显示错误消息“文件已损坏”.我怎样才能克服这个问题.

这是我的代码:

String lineEnd = "\r\n";            String twoHyphens = "--";            String boundary = "*****";        try {            // ------------------ CLIENT REQUEST            Log.e("Upload","InsIDe second Method");            fileinputStream fileinputStream = new fileinputStream(new file(sel_img_name));            // open a URL connection to the Servlet            URL url = new URL(IpAddress+"ImageUpload/"+token+"/"+name_sel_image+"%7C"+extension);            // 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);            DataOutputStream dos = new DataOutputStream(conn.getoutputStream());            dos.writeBytes(twoHyphens + boundary + lineEnd);            dos.writeBytes("Content-disposition: post-data; name=uploadedfile;filename="                            + name_sel_image + "" + lineEnd);            dos.writeBytes(lineEnd);            //Log.e(Tag,"headers are written");            // create a buffer of maximum size            int bytesAvailable = fileinputStream.available();            int maxBufferSize = 1000;            // int bufferSize = Math.min(bytesAvailable,maxBufferSize);            byte[] buffer = new byte[bytesAvailable];            // read file and write it into form...            int bytesRead = fileinputStream.read(buffer,bytesAvailable);            while (bytesRead > 0) {                dos.write(buffer,bytesAvailable);                bytesAvailable = fileinputStream.available();                bytesAvailable = Math.min(bytesAvailable,maxBufferSize);                bytesRead = fileinputStream.read(buffer,bytesAvailable);            }            // send multipart form data necesssary after file data...            dos.writeBytes(lineEnd);            dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);            // close streams            //Log.e(Tag,"file is written");            fileinputStream.close();            dos.flush();            dos.close();        } catch (MalformedURLException ex) {            Log.e(Tag,"error: " + ex.getMessage(),ex);        }        catch (IOException ioe) {            Log.e(Tag,"error: " + ioe.getMessage(),ioe);        }        try {            BufferedReader rd = new BufferedReader(new inputStreamReader(conn.getinputStream()));            String line;            while ((line = rd.readline()) != null) {                Log.i("Response",line);            }            rd.close();        } catch (IOException ioex) {            Log.e("MediaPlayer","error: " + ioex.getMessage(),ioex);        }
解决方法 不要写连字符,边界和lineEnd以查看您的服务器是否支持它.
    dos.writeBytes(twoHyphens boundary twoHyphens lineEnd);

我之前遇到过类似的问题,事实证明服务器正在使用连字符,lineEnd作为数据的一部分.

总结

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

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

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

原文地址:https://54852.com/web/1126650.html

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

发表评论

登录后才能评论

评论列表(0条)

    保存