问题
I am developing an web application which can upload/download a file from client to PostgreSQL database server via GWT RPC call.
I managed to create an upload servlet which store desired file(choosed by user via FileUpload widget) to Glassfish "TEMP" directory => then i used SQL command:
INSERT INTO table VALUES ('"+name+"',lo_import('"+f.getCanonicalPath()+"\\TEMP\\"+name+"'),...)
which put that file into database. This works pretty good.
Problem occurs when i want to download file from server to client. First i need to put the file back to TEMP dir with SQL command lo_export(...) -> this didn't work (some ERROR when creating a server file, permission denied), so i put the file manually to TEMP dir.
Question is how can i display that file which is stored on server in TEMP dir?
- my path to glassfish server temp dir:C:\Program Files (x86)\glassfish-3.1\glassfish\domains\domain1\TEMP\example.pdf
- when deploying app url looks like:
http://localhost:8080/AppName/ - i tried something like that: Window.open("http://localhost:8080/AppName/TEMP/example.pdf", "_blank", "enabled")
My CODE: Client side:
String link = GWT.getModuleBaseURL() + "filedownloadservlet";
Window.open(link,event.getSelectedItem().getText(),"enabled");
so i pass to servlet located on server side a link and a file name...am i right ?
Server side:
public class FileDownloadServlet extends HttpServlet {
private String path = "TEMP//"; // Your absolute path
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String filename = req.getParameter("filename");
System.out.println(filename); // THIS IS NULL value
File userManualFile = new File(path + filename);
// You can fetch a Blob from the database instead.
ServletOutputStream servletOutputStream = resp.getOutputStream();
resp.setContentType("application/pdf");
resp.addHeader("content-disposition", "attachment; filename=skuska.pdf");
FileInputStream fileInputStream = new FileInputStream(userManualFile);
IOUtils.copy(fileInputStream, servletOutputStream);
servletOutputStream.flush();
When i press a file in Tree widget it shows me a new browser window with this error:
java.io.FileNotFoundException: TEMP\null (The system cannot find the file specified)
回答1:
You cannot download a file with a RPC call. You must use a normal java servlet. You have to write the bytes into the HttpServletResponse. You can get the bytes from the file in the database by doing an SQL query.
This example is done with spring MVC.
@Controller
public class UserManualServlet {
private String path = "..." // Your absolute path
@RequestMapping("/app/usermanual.download")
public void generateInterceptActivationDeactivationReport(HttpServletRequest request, HttpServletResponse response)
throws IOException
{
String filename = request.getParameter("filename");
File userManualFile = new File(path + filename);
// You can fetch a Blob from the database instead.
ServletOutputStream servletOutputStream = response.getOutputStream();
response.setContentType("application/pdf");
response.addHeader("content-disposition", "attachment; filename=\"user-manual.pdf\"");
FileInputStream fileInputStream = new FileInputStream(userManualFile);
IOUtils.copy(fileInputStream, servletOutputStream);
servletOutputStream.flush();
}
In this example, you can call the URL : ../app/usermanual.download?filename=usermanual.pdf to download the file.
来源:https://stackoverflow.com/questions/9150218/gwt-get-url-of-file-located-on-server