Results 1 to 14 of 14
- 04-28-2008, 08:53 AM #1
Member
- Join Date
- Apr 2008
- Posts
- 9
- Rep Power
- 0
how to get a file from the server and save to disk of the client pc
Hi friends,
This is my first thread and I want to ask for help on my problem.
What I want to do is to be able to download a file residing on the application server pc and save it to the client pc where i am accesing the application. Can someone show me a sample code on how to go about this procedure. Thanks a lot..
- 04-28-2008, 08:56 AM #2
- 04-28-2008, 09:00 AM #3
Member
- Join Date
- Apr 2008
- Posts
- 9
- Rep Power
- 0
i havent started the project yet coz i want to understand the process first. I think i can understand if you can give me sample either in JSP or servlet.
- 04-28-2008, 09:03 AM #4
Member
- Join Date
- Apr 2008
- Posts
- 9
- Rep Power
- 0
the project that i want to make is for the clients to be able to get the generated report file that will be save to the server pc. sample file would be excel files or pdf. i believe type will only differ to the mime type so i am more interested how to be able to let the client get the file residing in the remote server pc.
- 04-28-2008, 09:22 AM #5
- 04-28-2008, 09:27 AM #6
Member
- Join Date
- Apr 2008
- Posts
- 9
- Rep Power
- 0
i was able to get a sample code but my problem is how to configure the code to my incorporate it to my servlet. i dont see error but i cant compile this servlet. I create a servlet form and paste this code but as ive said i cant compile it and i dont know why, and maybe someone can help me.
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
public class FileDownloader {
/** Creates a new instance of FileDownloader */
public FileDownloader() {
}
public void downloadFile(HttpServletResponse objHttpServletResponse, String strFileName) {
try {
File fExcelFile = new File(strFileName);
int dotIndex = strFileName.indexOf(".");
String fileType = strFileName.substring(dotIndex+1,strFileName.lengt h());
if (fileType.trim().equalsIgnoreCase("doc")) {
objHttpServletResponse.setContentType( "application/msword" );
} else if (fileType.trim().equalsIgnoreCase("xls")) {
objHttpServletResponse.setContentType( "application/vnd.ms-excel" );
} else if (fileType.trim().equalsIgnoreCase("pdf")) {
objHttpServletResponse.setContentType( "application/pdf" );
} else if (fileType.trim().equalsIgnoreCase("ppt")) {
objHttpServletResponse.setContentType( "application/ppt" );
} else {
objHttpServletResponse.setContentType( "application/octet-stream" );
}
objHttpServletResponse.setHeader("Content-Disposition", "attachment;filename=\"" + strFileName + "\"");
objHttpServletResponse.setHeader("Pragma", "public");
objHttpServletResponse.setHeader("Cache-control", "max-age=0");
InputStream objFileInputStream = new FileInputStream(fExcelFile);
ServletOutputStream objServletOutputStream = objHttpServletResponse.getOutputStream();
int iBit = 256;
try {
while(iBit >= 0) {
iBit = objFileInputStream.read();
objServletOutputStream.write(iBit);
}
} catch(IOException ioe) {
ioe.printStackTrace(System.out);
}
objServletOutputStream.flush();
objServletOutputStream.close();
objFileInputStream.close();
} catch(FileNotFoundException e) {
e.printStackTrace();
} catch(IOException e1) {
e1.printStackTrace();
}
}
}
- 04-28-2008, 09:28 AM #7
What error are you getting??
sanjeev,संजीव
- 04-28-2008, 09:29 AM #8
Member
- Join Date
- Apr 2008
- Posts
- 9
- Rep Power
- 0
the reports are already generated. its in xls and pdf format. what i want to do is to be able to download the report file and save it to the client pc.
- 04-28-2008, 09:31 AM #9
Member
- Join Date
- Apr 2008
- Posts
- 9
- Rep Power
- 0
type Exception report
message
descriptionThe server encountered an internal error () that prevented it from fulfilling this request.
exception
javax.servlet.ServletException: PWC1397: Wrapper cannot find servlet class trader.web.FileDownloader or a class it depends on
root cause
java.lang.ClassNotFoundException: trader.web.FileDownloader
note The full stack traces of the exception and its root causes are available in the Sun Java System Application Server 9.1_01 logs.
--------------------------------------------------------------------------------
Sun Java System Application Server 9.1_01
- 04-28-2008, 09:37 AM #10
Member
- Join Date
- Apr 2008
- Posts
- 9
- Rep Power
- 0
i imported the the trader.web package where i created the servlet and i got this error.
HTTP Status 500 -
--------------------------------------------------------------------------------
type Exception report
message
descriptionThe server encountered an internal error () that prevented it from fulfilling this request.
exception
javax.servlet.ServletException: PWC1403: Class trader.web.FileDownloader is not a Servlet
root cause
java.lang.ClassCastException: trader.web.FileDownloader cannot be cast to javax.servlet.Servlet
note The full stack traces of the exception and its root causes are available in the Sun Java System Application Server 9.1_01 logs.
--------------------------------------------------------------------------------
Sun Java System Application Server 9.1_01
- 04-28-2008, 09:43 AM #11
just extends your class with HttpServlet.
because your class is not a servletsanjeev,संजीव
- 04-28-2008, 09:46 AM #12
Member
- Join Date
- Apr 2008
- Posts
- 9
- Rep Power
- 0
oh i see, ok thanks sanjeev for your help
- 04-28-2008, 09:48 AM #13
Member
- Join Date
- Apr 2008
- Posts
- 9
- Rep Power
- 0
i got this another error message, what could be the problem here?
HTTP Status 405 - HTTP method GET is not supported by this URL
--------------------------------------------------------------------------------
type Status report
messageHTTP method GET is not supported by this URL
descriptionThe specified HTTP method is not allowed for the requested resource (HTTP method GET is not supported by this URL).
--------------------------------------------------------------------------------
Sun Java System Application Server 9.1_01
- 04-28-2008, 09:57 AM #14
Similar Threads
-
To transfer a file from client to server
By phani in forum NetworkingReplies: 4Last Post: 10-12-2010, 06:15 PM -
send file via client - server model
By spasavvas in forum NetworkingReplies: 15Last Post: 08-13-2010, 11:45 AM -
how to save image to disk after using pixelgrabber
By shishirg in forum Advanced JavaReplies: 5Last Post: 02-28-2009, 02:45 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks