java.lang.Object
java.io.OutputStream
javax.servlet.ServletOutputStream
- See Also:
- Top Examples, Source Code,
ServletResponse.getOutputStream()
public void print(boolean b)
throws IOException
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
[357]File Download Servlet
By Anonymous on 2005/04/07 12:57:00 Rate
import javax.servlet.http.*;
import javax.servlet.*;
import javax.servlet.ServletOutputStream;
import java.io.*;
public class FileDownloadServlet extends HttpServlet {
public void doGet ( HttpServletRequest req, HttpServletResponse res )
throws ServletException, IOException
{
String docSid = null;
String fullName = null;
String name = null;
ServletOutputStream out = res.getOutputStream ( ) ;
String fullName = "fileToBeDownloaded.xxx";
String pathName = getServletContext ( ) .getRealPath ( "/" + fullName ) ;
String contentType = getServletContext ( ) .getMimeType ( pathName ) ;
if ( contentType != null )
res.setContentType ( contentType ) ;
else
res.setContentType ( "application/octet-stream" ) ;
res.setHeader ( "Content-Disposition", "attachment; filename=\"" + name + "\"" ) ;
FileInputStream fis = null;
// Return the file
try {
fis = new FileInputStream ( fullName ) ;
byte [ ] buf = new byte [ 4 * 1024 ] ; // 4K buffer
int bytesRead;
while ( ( bytesRead = fis.read ( buf ) ) != -1 )
out.write ( buf, 0, bytesRead ) ;
}
catch ( FileNotFoundException e ) {
out.println ( "File not found: " + fullName ) ;
}
catch ( IOException e ) {
out.println ( "Problem sending file " + pathName + ": " + e.getMessage ( ) ) ;
}
finally {
if ( fis != null )
fis.close ( ) ;
}
}
}
public void print(char c)
throws IOException
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
public void print(double d)
throws IOException
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
public void print(float f)
throws IOException
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
public void print(int i)
throws IOException
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
public void print(String s)
throws IOException
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
public void print(long l)
throws IOException
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
public void println()
throws IOException
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
public void println(boolean b)
throws IOException
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
public void println(char c)
throws IOException
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
public void println(double d)
throws IOException
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
public void println(float f)
throws IOException
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
public void println(int i)
throws IOException
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
public void println(String s)
throws IOException
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
public void println(long l)
throws IOException
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
protected ServletOutputStream()
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
[672]Upload Servlet
By Anonymous on 2004/02/24 09:37:48 Rate
import java.io.BufferedInputStream;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class UploadServlet extends HttpServlet {
public void doPost ( HttpServletRequest request,
HttpServletResponse response )
throws ServletException, IOException {
ServletOutputStream out = response.getOutputStream ( ) ;
BufferedInputStream in =
new BufferedInputStream ( request.getInputStream ( ) ) ;
out.println ( request.getHeader ( "content-type" ) ) ;
int c = -1;
while ( ( c=in.read ( ) ) > = 0 ) out.write ( c ) ;
out.close ( ) ;
}
}
[1127]Download file
By Khurram Chaudhry on 2004/11/19 15:47:18 Rate
response.setContentType ( "application/download" ) ;
response.setHeader (
"Content-disposition",
"filename=" + FILE_NAME ) ;
byte [ ] b = rep.getBytes ( ) ;
response.getOutputStream ( ) .write ( b, 0, b.length ) ;