Hi,
This is my code...
/* @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ */
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.net.*;
public class SendFileN extends HttpServlet {
public void init(ServletConfig conf) throws ServletException {
super.init(conf);
}
public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException{
// ##########################################################################
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
String fileURL="http://localhost/webstore/resources/java/app/CConverter111111.jad";
try {
URL url =new URL(fileURL);
URLConnection connection = url.openConnection();
connection.setDoInput( true );
connection.setDoOutput( true );
res.setContentType("text/plain");
res.setHeader("Content-disposition", "attachment; filename=CConverter111111.jad");
ServletOutputStream out = res.getOutputStream();
bis = new BufferedInputStream(connection.getInputStream());
bos = new BufferedOutputStream(out);
byte[] buff = new byte[2048];
int bytesRead;
while(-1 != (bytesRead = bis.read(buff, 0, buff.length))) {
bos.write(buff, 0, bytesRead);
}
out.println();
return;
} catch(final MalformedURLException e) {
System.out.println ( "MalformedURLException." );
throw e;
} catch(final IOException e) {
System.out.println ( "IOException." );
throw e;
} finally {
if (bis != null)
bis.close();
if (bos != null)
bos.close();
}
}
}
/* @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ */
When I run this servlet sometime I am getting the expected result. But some time it prints the file contents in the browser itself.
Please fix this.
Thank you in advance.