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

标签 android image-uploading

enter image description here我正在做一个项目,我必须使用 URL 将图像上传到服务器。 但是当我上传文件时,服务器中的文件已损坏,当我打开文件时,它显示一条错误消息,“文件已损坏”。我怎样才能克服这个问题。

这是我的代码:

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, 0, bytesAvailable);

            while (bytesRead > 0) {
                dos.write(buffer, 0, bytesAvailable);
                bytesAvailable = fileInputStream.available();
                bytesAvailable = Math.min(bytesAvailable, maxBufferSize);
                bytesRead = fileInputStream.read(buffer, 0, 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);
        }

最佳答案

不要写连字符、boundary 和 lineEnd 看看你的服务器是否支持。 dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);

我之前遇到过类似的问题,结果是服务器将连字符、lineEnd 作为数据的一部分。

关于android图像上传,文件在服务器中损坏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11823664/

相关文章:

c# - 将 Base64 格式的图像发送到 IIS 服务器(托管基于 C#.net 的 Web 处理程序)

php - 使用 PHP、Laravel、GD 上传图像并将多种尺寸保存到文件系统

ios - 上传图片到服务器

android - 如何在 Android 的 Google map 中的两点之间画一条线?

android - 'kotlin-android-extensions' Gradle 插件已弃用

android - 我如何启用和禁用 android :singleLine dynamically?

node.js - aws s3 node js sdk方法generatePutUrl为本地主机和heroku上的部署返回不同的结果

android - android中窗口管理器服务的疑问

java - 适用于 Android 的 Lib Panorama Spherical

ios - 上传图片时出现问题