将文件(txt,docs,ppt)上传到服务器

时间:2014-07-30 06:50:32

标签: android file-upload

我正在创建一个应用,用户选择一个文件并将其上传到服务器。如果用户从图库中选择图像,图像正在正确上传,但是当用户选择文件如txt或word文件时我系统内存异常。

上传文件的代码

protected String sendFile(final Context context, final String url, final String params, String fileName, final File f) {
        DataOutputStream dos = null;
        int bytesRead = 0, bytesAvailable = 0, bufferSize;
        int maxBufferSize = 1024;
        byte[] buffer;
        String serverResponseMessage = null;
        String uploaded_file = fileName;


        URL url1 = null;
        try {

            FileInputStream fileInputStream = new FileInputStream(f);
            url1 = new URL(url + "?" + params);
            HttpURLConnection connection = (HttpURLConnection) url1.openConnection();
            connection.setRequestMethod("POST");
            connection.setDoInput(true);
            connection.setDoOutput(true);
            connection.setUseCaches(false);
            connection.setRequestProperty("Connection", "Keep-Alive");
            connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
            connection.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + boundary);
            connection.setRequestProperty("uploaded_file", fileName);
            fileName = f.getName();
            dos = new DataOutputStream(connection.getOutputStream());
            dos.writeBytes(twoHyphens + boundary + lineEnd);

            dos.writeBytes("Content-Disposition: form-data; name=\"uploaded_file\";filename=\"" + fileName + "\"" + lineEnd);
            dos.writeBytes(lineEnd);


            bytesAvailable = fileInputStream.available(); // create a buffer of  maximum size

            bufferSize = Math.min(bytesAvailable, maxBufferSize);
            buffer = new byte[bufferSize];

            // read file and write it into form...
            bytesRead = fileInputStream.read(buffer, 0, bufferSize);

            while (bytesRead > 0) {
                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);

            dos.close();

            // Responses from the server (code and message)
            int serverResponseCode = connection.getResponseCode();
            serverResponseMessage = connection.getResponseMessage();

            StringBuffer b;
            int ch;
            InputStream is;
            is = connection.getInputStream();
            // retrieve the response from server

            b = new StringBuffer();
            while ((ch = is.read()) != -1) {
                b.append((char) ch);
            }
            response = b.toString();
            Log.e("Response", "" + response);


        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (ProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
            Log.e("Error message", "" + e);
        }
        return response;
    }

选择文件的代码

if (requestCode == 2 && resultCode == getActivity().RESULT_OK) {
            strFilePath = data.getData().getPath();


        }

logcat的

07-30 12:15:03.069    7829-8085/example.com.pocketdocs E/Upload file response﹕ System.OutOfMemoryException: Out of memory.
            at System.Drawing.Image.FromFile(String filename, Boolean useEmbeddedColorManagement)
            at Web_Account_UploadFile.Page_Load(Object sender, EventArgs e) in C:\APP\PocketDocs\PocketDocs\PocketDocsWeb\web\Account\UploadFile.aspx.vb:line 3753

ASP代码

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim Ret As String = ""
        Try
            If Request.Files.Count > 0 Then

                'Dim Posted_File As HttpPostedFile
                'Posted_File = Request.Files.Get(0)

                Dim objPocketDocs As New PocketdocsEntities
                Dim ListDocID As Decimal = objPocketDocs.ins_doc_details(Request.QueryString("User_ID"), Posted_File.FileName, Path.GetExtension(Posted_File.FileName), Request.QueryString("User_ID"), Request.QueryString("FileTitle").ToString, Request.QueryString("DT")).FirstOrDefault


                If Not My.Computer.FileSystem.DirectoryExists(Server.MapPath("\IMSAFE") & "\TripImages") Then
                    My.Computer.FileSystem.CreateDirectory(Server.MapPath("\IMSAFE") & "\TripImages")
                End If
                Dim orifilename As String
                orifilename = Server.MapPath("/Doc_Files/") & Request.QueryString("User_ID") & "_" & ListDocID & Path.GetExtension(Posted_File.FileName) & Path.GetExtension(Posted_File.FileName)


                If File.Exists(orifilename) Then
                    File.Delete(orifilename)

为什么图片正在正常上传,但上传文件会给我带来错误????

0 个答案:

没有答案