0

I have a method which start download of text file that is stored in oracle.

The column type is BLOB. I'm using this code below to init download, but I have no idea how set encode to this file when client downloaded it.

if (result.next()) {

        String fileName0 = String.valueOf(result.getDate(columnData));
        String fileName1 = String.valueOf(result.getInt(columnNumSolit));
        String fileName2 = String.valueOf(result.getInt(columnNumComplto));
        BLOB blob = ((OracleResultSet) result).getBLOB(columnFile);
        InputStream inputStream = blob.getBinaryStream();
        //int fileLength = inputStream.available();
        int fileLength = blob.getChunkSize();

        ServletContext context = getServlet().getServletContext();

        // Set MIME to  file.
        String mimeType = context.getMimeType(fileName0+fileName1+fileName2+ext);
        if (mimeType == null) {        
            mimeType = "application/octet-stream";
        }              

        //  header to response.
        response.setContentType(mimeType);
        response.setContentLength(fileLength);
        String headerKey = "Content-Disposition";
        String headerValue = String.format("attachment; filename=\"%s\"",fileName0+fileName1+fileName2+ext);
        response.setHeader(headerKey, headerValue);

        // write file to client.
        OutputStream outStream = response.getOutputStream();


        byte[] buffer = new byte[fileLength];
        int bytesRead = -1;

        while ((bytesRead = inputStream.read(buffer)) != -1) {
            outStream.write(buffer, 0, bytesRead);

        }


        inputStream.close();
        outStream.close();

        tipoSolit = null;
    }else {
        //do something

    }
danillonc
  • 49
  • 1
  • 7

1 Answers1

0

What behavior are you trying to get the browser to do? Are you sending a video or mp3 or what? The mimetype is set by the headers you send before the file. If know of a website that has the behavior you are looking for, you can simply view what headers they send in chrome or in wireshark and then just use those in your own code.

One of the problems with file-type encoding is that you need to keep the encoding correct for each step that the bytes take. This means that if any step in the "upload->storage in db->fetching db->download" removes utf-8 encoding, you get the wrong answer. I wrote a post about my travels with UTF-8 and MySQL here: https://stackoverflow.com/a/14411280/836450 I would suggest sending a file that has a single utf-8 character, and then debugging the entire chain by hand.

Community
  • 1
  • 1
benathon
  • 7,455
  • 2
  • 41
  • 70