为什么它会返回内部服务器错误的HttpURLConnection类? [英] Why it returns Internal Server Error on httpUrlConnection?

查看:177
本文介绍了为什么它会返回内部服务器错误的HttpURLConnection类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发,可以不同的文件发送到网络应用程序server.Also我想发送大文件,在能够做到这一点,我需要大块的文件。但是,当我发送文件到服务器没有被上传。我不知道如果我是如何发送文件有错误,它给我的response.I错误500(内部服务器错误)不认为服务器是因为当我使用上传文件的问题multiPartEntity它的工作原理,但林使用的BufferedInputStream和DataOutputStream当它不工作。请帮助我,告诉什么是错的我的code,为什么不能把我的文件。下面是我到目前为止:

 字符串samplefile =存储/ sdcard0 /图片/图片/ picture.jpg
        文件MFILE =新的文件(samplefile);        INT mychunkSize = 2048 * 1024;
        最终长尺寸= mFile.length();
        最终长块=大小和LT; mychunkSize? 1:(mFile.length()/ mychunkSize);        INT chunkId = 0;
        尝试{            的BufferedInputStream流=新的BufferedInputStream(新的FileInputStream(MFILE));            字符串lineEnd =\\ r \\ n;
            串twoHyphens = - ;
            字符串边界=------------------------- acebdf13572468; //随机数据            对于(chunkId = 0; chunkId<块; chunkId ++){                 网址URL =新的URL(urlString);                 //打开HTTP连接的URL
                 HttpURLConnection的康恩=(HttpURLConnection类)url.openConnection();                 conn.setReadTimeout(20000 / *毫秒* /);
                 conn.setConnectTimeout(20000 / *毫秒* /);
                 //允许输入
                 conn.setDoInput(真);
                 //允许输出
                 conn.setDoOutput(真);
                 //不要使用缓存副本。
                 conn.setUseCaches(假);
                 //使用POST方法。
                 conn.setRequestMethod(POST);                 字符串连接codeD = Base64.en codeToString((_用户名+:+ _密码).getBytes(),Base64.NO_WRAP);
                 conn.setRequestProperty(授权,基本+ EN codeD);
                 conn.setRequestProperty(连接,保持活动);                 conn.setRequestProperty(内容类型,的multipart / form-data的;边界=+边界);
                 DataOutputStream类DOS =新的DataOutputStream类(conn.getOutputStream());
                 dos.writeBytes(twoHyphens +边界+ lineEnd);                 串的param1 =+ chunkId;
                 字符串参数2 =+块;
                 串参数3 = mFile.getName();
                 字符串param4 = samplefile;              //发送参数#FILE
                dos.writeBytes(内容处置:表格数据;名称= \\fieldNameHere \\;文件名= \\+参数3 +\\+ lineEnd); //文件名的文件的名字上传
                dos.writeBytes(内容类型:image / JPEG+ lineEnd);
                dos.writeBytes(lineEnd);                //发送参数#chunks
                dos.writeBytes(内容处置:表格数据;名称= \\块\\+ lineEnd);
                dos.writeBytes(内容类型:text / plain的;字符集= UTF-8+ lineEnd);
                dos.writeBytes(内容长度:+ param2.length()+ lineEnd);
                dos.writeBytes(lineEnd);
                dos.writeBytes(参数2 + lineEnd);
                dos.writeBytes(twoHyphens +边界+ lineEnd);
                //发送参数#NAME
                dos.writeBytes(内容处置:表格数据;名称= \\名称\\+ lineEnd);
                dos.writeBytes(内容类型:text / plain的;字符集= UTF-8+ lineEnd);
                dos.writeBytes(内容长度:+ param3.length()+ lineEnd);
                dos.writeBytes(lineEnd);
                dos.writeBytes(参数3 + lineEnd);
                dos.writeBytes(twoHyphens +边界+ lineEnd);
                字节[]缓冲区=新的字节[mychunkSize]                stream.read(缓冲液);                dos.write(缓冲液);                dos.writeBytes(lineEnd);
                dos.writeBytes(twoHyphens +边界+ twoHyphens + lineEnd);
                dos.flush();
                dos.close();
            }
        }赶上(例外五){
            Log.e(错误上传文件,e.toString());
        }


解决方案

如果您preFER那么你可以使用这个库。它很容易实现和剂量所有你需要的工作。

AndroidAsync

I'm developing an application that can send different files to web server.Also I want to send large files, in able to do this I need to chunk the files. But when I'm sending the files to server nothing is uploaded. I don't know if there's error on how I'm sending the files and it gives an error 500 (internal server error) on my response.I don't think the server is the problem because when I'm uploading a file using multiPartEntity it works but when Im using BufferedInputStream and DataOutputStream it doesn't work. Please help me and tell what's wrong on my code why it can't send my files. Here's I got so far:

        String samplefile = "storage/sdcard0/Pictures/Images/picture.jpg";
        File mFile = new File(samplefile);

        int mychunkSize = 2048 * 1024;
        final long size = mFile.length();
        final long chunks = size < mychunkSize? 1: (mFile.length() / mychunkSize);

        int chunkId = 0;
        try {

            BufferedInputStream stream = new BufferedInputStream(new FileInputStream(mFile));

            String lineEnd = "\r\n";
            String twoHyphens = "--";
            String boundary =  "-------------------------acebdf13572468";// random data

            for (chunkId = 0; chunkId < chunks; chunkId++) {

                 URL url = new URL(urlString);

                 // Open a HTTP connection to the URL
                 HttpURLConnection conn = (HttpURLConnection) url.openConnection();

                 conn.setReadTimeout(20000 /* milliseconds */);
                 conn.setConnectTimeout(20000 /* milliseconds */);


                 // 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");

                 String encoded = Base64.encodeToString((_username+":"+_password).getBytes(),Base64.NO_WRAP); 
                 conn.setRequestProperty("Authorization", "Basic "+encoded); 
                 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);

                 String param1 = ""+chunkId;
                 String param2 = ""+chunks;
                 String param3 = mFile.getName();
                 String param4 = samplefile;

              // Send parameter #file
                dos.writeBytes("Content-Disposition: form-data; name=\"fieldNameHere\";filename=\"" + param3 + "\"" + lineEnd); // filename is the Name of the File to be uploaded
                dos.writeBytes("Content-Type: image/jpeg" + lineEnd);
                dos.writeBytes(lineEnd);



                // Send parameter #chunks
                dos.writeBytes("Content-Disposition: form-data; name=\"chunk\"" + lineEnd);
                dos.writeBytes("Content-Type: text/plain; charset=UTF-8" + lineEnd);
                dos.writeBytes("Content-Length: " + param2.length() + lineEnd);
                dos.writeBytes(lineEnd);
                dos.writeBytes(param2 + lineEnd);
                dos.writeBytes(twoHyphens + boundary + lineEnd);


                // Send parameter #name
                dos.writeBytes("Content-Disposition: form-data; name=\"name\"" + lineEnd);
                dos.writeBytes("Content-Type: text/plain; charset=UTF-8" + lineEnd);
                dos.writeBytes("Content-Length: " + param3.length() + lineEnd);
                dos.writeBytes(lineEnd);
                dos.writeBytes(param3 + lineEnd);
                dos.writeBytes(twoHyphens + boundary + lineEnd);


                byte[] buffer = new byte[mychunkSize];

                stream.read(buffer);

                dos.write(buffer);

                dos.writeBytes(lineEnd);
                dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);
                dos.flush();
                dos.close();


            }
        } catch (Exception e) {
            Log.e("Error Uploading Files", e.toString());
        }

解决方案

If you prefer then you can use this library. Its very easy to implement and dose all the work you need.

AndroidAsync

这篇关于为什么它会返回内部服务器错误的HttpURLConnection类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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