The Artima Developer Community
Sponsored Link

Java Answers Forum
File Download Servlet

0 replies.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a flat view of this topic  Flat View
Previous Topic   Next Topic
Threaded View: This topic has 0 replies on 1 page
Greg Machos

Posts: 1
Nickname: hurctrack
Registered: Oct, 2003

File Download Servlet Posted: Oct 27, 2003 2:20 PM
Reply to this message Reply
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();
}

}
}


Topic: help me in my choice Previous Topic   Next Topic Topic: Array of classes HELP!!!

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use