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;
}
%>