Greg Machos
Posts: 1
Nickname: hurctrack
Registered: Oct, 2003
|
|
File Download Servlet
|
Posted: Oct 27, 2003 2:20 PM
|
|
Good evening. I was wondering if someone could help me with a Download Servlet our company uses. I have supplied the code below. It is used to download PowerPoint files.
The problem with it is that the servlet performs very slowly when trying to download a file over our local network. Our network is a 10/100 Mbps LAN, and the maximum file size for a PowerPoint file is 4.6 MB, and it takes about 11 to 12 minutes to download. Does anyone know what could be causing the problem? Let me know. Thanks.
Here is the code:
package com.brightideas.misc;
import javax.servlet.*; import javax.servlet.http.*; import java.io.*; import lotus.notes.*;
public class DownloadServlet extends HttpServlet {
static final int MAXIMUM_BUFFER_SIZE = 4096;
public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
Session session; Database db; Document doc; String dbString = request.getParameter("db"); String unid = request.getParameter("unid"); String filename = request.getParameter("file"); String target = request.getParameter("target"); EmbeddedObject attachment; File directory; File detachedFile; String filePath; ServletOutputStream out = response.getOutputStream();
if (dbString == null || dbString.equals("") || unid == null || unid.equals("")) { //ServletOutputStream out = response.getOutputStream(); System.out.println("The full path the file must be specified"); out.println("The full path the file must be specified"); return; } if (null == filename || filename.equals("")) { //use unid as the default filename filename = unid; } if (null == target || target.equals("")) { //attachment filename as used for content-disposition unless a target filename is specified target = filename; } try { NotesThread.sinitThread(); session = Session.newInstance(); //get handle to doc db = session.getDatabase("", dbString); if (db == null || !db.isOpen()) { //ServletOutputStream out = response.getOutputStream(); System.out.println("Unable to open database"); out.println("Unable to open database"); return; } doc = db.getDocumentByUNID(unid); if (doc == null) { //ServletOutputStream out = response.getOutputStream(); System.out.println("Unable to open document"); out.println("Unable to open document"); return; }
//extract file attachment = doc.getAttachment(filename); if (attachment == null) { //ServletOutputStream out = response.getOutputStream(); System.out.println("Unable to open attachment"); out.println("Unable to open attachment"); return; } directory = new File(unid); if (!directory.exists()) { directory.mkdir(); } filePath = directory.getPath() + "/" + filename; attachment.extractFile(filePath); db.recycle(); detachedFile = new File(filePath); //String filename = req.getParameter("db") + ".ppt"; //String filepath = "C:\\tomcat331\\webapps\\examples\\uploadpres\\"; response.setContentType("application/unknown"); response.setHeader( "Content-Disposition", "attachment; filename=\"" + target + "\""); long fileLength = detachedFile.length(); response.setHeader( "Content-Length", new Long(fileLength).toString()); //response.setContentLength( new Long( detachedFile.length() ).intValue() ) ; // print the file InputStream in = null; int bufferSize = 0 ; if ( fileLength > MAXIMUM_BUFFER_SIZE ){ bufferSize = MAXIMUM_BUFFER_SIZE ; } else { bufferSize = (int) fileLength ; } try { in = new BufferedInputStream(new FileInputStream(filePath), bufferSize ); int ch; while ((ch = in.read()) != -1) { out.write(ch); } } finally { if (in != null) in.close(); // very important }
/* java.io.FileInputStream fileInputStream = new java.io.FileInputStrea m(filePath); int i; while ((i = fileInputStream.read()) != -1) { out.write(i); } fileInputStream.close(); out.close(); */ detachedFile.delete(); directory.delete();
} catch (NotesException e) { getServletContext().log(e.getMessage()); e.printStackTrace(); } finally { NotesThread.stermThread(); }
} }
|
|