hi all,
in the below code is used to open a file with its corresponding application.the file with application is opening but the data is corrupted. pls help to open the file without data corruption.
advance thanks

<%
// fname is the file name
String filePath=savefile;
File f = new File(filePath);
InputStream in = new FileInputStream(f);
ServletOutputStream outs = response.getOutputStream();

        int bufferSize = 4096;
        byte[] ioBuffer = new byte[bufferSize];
        byte[] lastBuffer; // last buffer
        long counter = 0; // how many bytes we have gotten through
        long fileSize = f.length();
      String fileType = filePath.substring(filePath.indexOf(".")+1);
      out.println("filetype  ::"+filePath.indexOf("."));
      out.println("filetype  ::"+filePath.length());
      out.println("filetype  ::"+fileType);
        if (fileType.trim().equalsIgnoreCase("txt")) {
            out.println("filetype  ::"+fileType);

            response.setContentType( "text/plain" );
        } else if (fileType.trim().equalsIgnoreCase("doc")) {
            out.println("doc  ::"+fileType);

            response.setContentType( "application/msword" );
        } else if (fileType.trim().equalsIgnoreCase("xls")) {
            out.println("xls  ::"+fileType);

            response.setContentType( "application/ms-excel" );
        } else if (fileType.trim().equalsIgnoreCase("pdf")) {
            out.println("pdf  ::"+fileType);

            response.setContentType( "application/pdf" );
            //log.debug("content type set to pdf");
        } 
         else if (fileType.trim().equalsIgnoreCase("indd")) {
             out.println("indd  ::"+fileType);
             response.setContentType( "application/indd" );
        }
        else
        {
            response.setContentType( "application/octet-stream" );
        }
        response.setContentLength((int)f.length());

response.setHeader("Content-Disposition", "attachment; filename="+ java.net.URLEncoder.encode(filePath) +"");
//response.setContentType("application/download");
response.setHeader("Content-Transfer-Encoding", "7bit");

// in a big file, use big buffer chunks
while (fileSize > (counter + bufferSize))
{
    in.read(ioBuffer);
    outs.write(ioBuffer);
           counter += bufferSize;
}   
%>

Wow, you seem to have finally taken the hint. Good job. But, back to this code.

1) you should use (fileSize > counter) otherwise you will wind up skipping the last bit of data in the file.

2) read(byte[]) is not guaranteed to fully read the entire size of the array. It does however return the number of bytes read, so capture this value.

3) Make sure that you only write as much of the array as was read (see above). Otherwise you may be writing old data again (or at least null bytes, which is nearly as bad).

4) Increment the counter with the amount that was actually read (see 2) as well.

Edit: Except for the fact that this should not be done as a Scriptlet in a JSP. Makes much more sense to do this one in a Servlet.

commented: Very impressive - you took out time to read that guys code; awesomeness :) +2

hi masijade,
thanks a lot man. it is working fine yaar. very very thanks. u have good knowledge.
keep it up ur helping tendency.
with regards
hidash.

help me to close a application using java.
is there any idea means pls tell.
advance thanks......

As I have told you already (maybe you still haven't gotten it after all), you are not "opening" anything with this code. You are simply telling the browser what it is to display (you do this by setting the content-type and content disposition) and then feeding the data through.

The browser is deciding what to do with it (i.e. how to display it). Java and your JSP has no control over this. From this point on it all determined, and controlled, by the browser, on the client. The server and the JSP has nothing more to do with it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.