欢迎访问悦橙教程(wld5.com),关注java教程。悦橙教程  java问答|  每日更新
页面导航 : > > 文章正文

后台上传大图片,后台上传图片,int maxBuffe

来源: javaer 分享于  点击 36685 次 点评:43

后台上传大图片,后台上传图片,int maxBuffe


int maxBufferSize = 1*256*1024;int headerSize=89;String urlString = "http://xxxxxxxx.com/upload.php";int fileSize=0;int infoSize=0;protected Integer doInBackground(String... file) {            Integer ret=0;            HttpURLConnection conn = null;            DataOutputStream dos = null;            DataInputStream inStream = null;            String exsistingFileName = file[0];            String lineEnd = "";            String twoHyphens = "--";            String boundary =  "*****";            int bytesRead, bytesAvailable, bufferSize;            byte[] buffer;                      try            {                Log.i("MyAPP","Start uploading file: " + file[0]);                FileInputStream fileInputStream = new FileInputStream(                    new File(exsistingFileName));                // open a URL connection to the Servlet                 URL url = new URL(urlString);                //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.                fileSize=fileInputStream.available();                infoSize=fileSize + headerSize + file[0].length();                conn.setFixedLengthStreamingMode(infoSize);                conn.setRequestMethod("POST");                conn.setRequestProperty("Connection", "Keep-Alive");                conn.setRequestProperty("Content-Type",                 "multipart/form-data;boundary="+boundary);                dos = new DataOutputStream( conn.getOutputStream() );                dos.writeBytes(twoHyphens + boundary + lineEnd);                dos.writeBytes("Content-Disposition: form-data;                 name='uploadedfile';filename='" + file[0] + "'" + lineEnd);                dos.writeBytes(lineEnd);                // create a buffer of maximum size                bytesAvailable = fileInputStream.available();                bufferSize = Math.min(bytesAvailable, maxBufferSize);                buffer = new byte[bufferSize];                // read file and write it into form...                bytesRead = fileInputStream.read(buffer, 0, bufferSize);                int bytesSent=0;                while (bytesRead > 0)                {                    if (bytesSent > 0){                        int pg=(bytesSent*100)/infoSize;                        publishProgress(pg);                    }                    bytesSent+=bufferSize;                    dos.write(buffer, 0, bufferSize);                    bytesAvailable = fileInputStream.available();                    bufferSize = Math.min(bytesAvailable, maxBufferSize);                    bytesRead = fileInputStream.read(buffer, 0, bufferSize);                }                // send multipart form data necesssary after file data...                dos.writeBytes(lineEnd);                dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);                //close streams                fileInputStream.close();                dos.flush();                dos.close();            }            catch (MalformedURLException ex)            {                Log.e("MyAPP", "error: " + ex.getMessage(), ex);                ret=1;            }            catch (IOException ioe)            {                Log.e("MyAPP", "error: " + ioe.getMessage(), ioe);                ret=1;            }            try {                inStream = new DataInputStream ( conn.getInputStream() );                String str;                while (( str = inStream.readLine()) != null)                {                    Log.i("MyAPP","Server Response"+str);                }                inStream.close();            }            catch (IOException ioex){                Log.e("MyAPP", "error: " + ioex.getMessage(), ioex);                ret=1;            }            return ret;        }
相关栏目:

用户点评