aggiunto servlet dedicato per il download dei file
This commit is contained in:
@@ -55,7 +55,7 @@ public class PackageTable extends WTable
|
|||||||
img.setStyleClass("download-icon");
|
img.setStyleClass("download-icon");
|
||||||
img.setWidth(new WLength("16px"));
|
img.setWidth(new WLength("16px"));
|
||||||
button.setImage(img);
|
button.setImage(img);
|
||||||
button.setLink(new WLink(WLink.Type.Url, "rest/pkg/download/" + pkg.fileName));
|
button.setLink(new WLink(WLink.Type.Url, "archive/" + pkg.fileName));
|
||||||
button.addStyleClass("text-center");
|
button.addStyleClass("text-center");
|
||||||
new WText("Download",button);
|
new WText("Download",button);
|
||||||
getElementAt(row, 5).addWidget(button);
|
getElementAt(row, 5).addWidget(button);
|
||||||
|
68
src/main/java/org/jpacrepo/frontend/servlet/FileServlet.java
Normal file
68
src/main/java/org/jpacrepo/frontend/servlet/FileServlet.java
Normal file
@@ -0,0 +1,68 @@
|
|||||||
|
package org.jpacrepo.frontend.servlet;
|
||||||
|
|
||||||
|
import org.jpacrepo.context.ApplicationContext;
|
||||||
|
import org.jpacrepo.context.DefaultConfiguration;
|
||||||
|
|
||||||
|
import javax.inject.Inject;
|
||||||
|
import javax.servlet.ServletContext;
|
||||||
|
import javax.servlet.ServletException;
|
||||||
|
import javax.servlet.ServletOutputStream;
|
||||||
|
import javax.servlet.annotation.WebServlet;
|
||||||
|
import javax.servlet.http.HttpServlet;
|
||||||
|
import javax.servlet.http.HttpServletRequest;
|
||||||
|
import javax.servlet.http.HttpServletResponse;
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.FileInputStream;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.InputStream;
|
||||||
|
import java.util.logging.Logger;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Created by walter on 29/07/15.
|
||||||
|
*/
|
||||||
|
|
||||||
|
|
||||||
|
@WebServlet("/archive/*")
|
||||||
|
public class FileServlet extends HttpServlet
|
||||||
|
{
|
||||||
|
@Inject
|
||||||
|
@DefaultConfiguration
|
||||||
|
private ApplicationContext ctx;
|
||||||
|
|
||||||
|
private Logger log = Logger.getLogger(FileServlet.class.getName());
|
||||||
|
|
||||||
|
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
|
||||||
|
{
|
||||||
|
String fileName = request.getPathInfo();
|
||||||
|
if (fileName == null || fileName.equals(""))
|
||||||
|
{
|
||||||
|
throw new ServletException("File Name can't be null or empty");
|
||||||
|
}
|
||||||
|
File file = new File(ctx.getSystemProperties().getProperty("RepoFolder"), fileName);
|
||||||
|
if (!file.exists())
|
||||||
|
{
|
||||||
|
response.sendError(HttpServletResponse.SC_NOT_FOUND);
|
||||||
|
//response.sendRedirect("../404.xhtml");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
log.fine("File location on server::" + file.getAbsolutePath());
|
||||||
|
ServletContext ctx = getServletContext();
|
||||||
|
InputStream fis = new FileInputStream(file);
|
||||||
|
String mimeType = ctx.getMimeType(file.getAbsolutePath());
|
||||||
|
response.setContentType(mimeType != null ? mimeType : "application/octet-stream");
|
||||||
|
response.setContentLength((int) file.length());
|
||||||
|
response.setHeader("Content-Disposition", String.format("attachment; filename=\"%s\"", fileName));
|
||||||
|
|
||||||
|
ServletOutputStream os = response.getOutputStream();
|
||||||
|
byte[] bufferData = new byte[1024];
|
||||||
|
int read;
|
||||||
|
while ((read = fis.read(bufferData)) != -1)
|
||||||
|
{
|
||||||
|
os.write(bufferData, 0, read);
|
||||||
|
}
|
||||||
|
os.close();
|
||||||
|
fis.close();
|
||||||
|
log.fine("File successfully downloaded to client");
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@@ -15,7 +15,7 @@ import javax.servlet.annotation.WebServlet;
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
||||||
@WebServlet(name="app.CherebaServlet", urlPatterns={"/view/*"})
|
@WebServlet(urlPatterns = {"/view/*"})
|
||||||
public class JPacRepoServlet extends WtServlet
|
public class JPacRepoServlet extends WtServlet
|
||||||
{
|
{
|
||||||
@Inject
|
@Inject
|
||||||
|
@@ -11,7 +11,12 @@
|
|||||||
<description>jpacrepo</description>
|
<description>jpacrepo</description>
|
||||||
|
|
||||||
<!--<session-config>-->
|
<!--<session-config>-->
|
||||||
<!--<tracking-mode>URL</tracking-mode>-->
|
<!--<tracking-mode>URL</tracking-mode>-->
|
||||||
<!--</session-config>-->
|
<!--</session-config>-->
|
||||||
|
|
||||||
|
<error-page>
|
||||||
|
<error-code>404</error-code>
|
||||||
|
<location>404.xhtml</location>
|
||||||
|
</error-page>
|
||||||
|
|
||||||
</web-app>
|
</web-app>
|
||||||
|
79
src/main/webapp/404.xhtml
Normal file
79
src/main/webapp/404.xhtml
Normal file
@@ -0,0 +1,79 @@
|
|||||||
|
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
|
||||||
|
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
|
||||||
|
<html xmlns="http://www.w3.org/1999/xhtml">
|
||||||
|
|
||||||
|
|
||||||
|
<head>
|
||||||
|
|
||||||
|
|
||||||
|
</head>
|
||||||
|
|
||||||
|
<body>
|
||||||
|
|
||||||
|
|
||||||
|
<table>
|
||||||
|
<tbody>
|
||||||
|
<tr>
|
||||||
|
<td id="tableProps" align="left" valign="top"><img id="pagerrorImg"
|
||||||
|
src="http://www.larknews.com/images/pageerror.gif"
|
||||||
|
height="33" width="25"></td>
|
||||||
|
<td id="tableProps2" align="left" valign="middle" width="360"><h1 id="errortype"
|
||||||
|
style="COLOR: black; FONT: 13pt/15pt verdana">
|
||||||
|
<span id="errorText">The page cannot be found</span></h1>
|
||||||
|
</td>
|
||||||
|
</tr>
|
||||||
|
<tr>
|
||||||
|
<td id="tablePropsWidth" colspan="2" width="400"><font style="COLOR: black; FONT: 8pt/11pt verdana">Possible
|
||||||
|
causes:</font></td>
|
||||||
|
</tr>
|
||||||
|
<tr>
|
||||||
|
<td id="tablePropsWidth2" colspan="2" width="400"><font id="LID1" style="COLOR: black; FONT: 8pt/11pt verdana">
|
||||||
|
<hr color="#C0C0C0" noshade="">
|
||||||
|
<ul>
|
||||||
|
<li id="list1"><span class="infotext"><strong>Baptist explanation: </strong>There
|
||||||
|
must be sin in your life. Everyone else opened it fine.<br>
|
||||||
|
</span></li>
|
||||||
|
<li><span class="infotext"> <strong>Presbyterian explanation: </strong>It's
|
||||||
|
not God's will for you to open this link.<br>
|
||||||
|
</span></li>
|
||||||
|
<li><span class="infotext"><strong> Word of Faith explanation:</strong>
|
||||||
|
You lack the faith to open this link. Your negative words have prevented
|
||||||
|
you from realizing this link's fulfillment.<br>
|
||||||
|
</span></li>
|
||||||
|
<li><span class="infotext"><strong>Charismatic explanation: </strong>Thou
|
||||||
|
art loosed! Be commanded to OPEN!<br>
|
||||||
|
</span></li>
|
||||||
|
<li><span class="infotext"><strong>Unitarian explanation:</strong> All
|
||||||
|
links are equal, so if this link doesn't work for you, feel free to
|
||||||
|
experiment with other links that might bring you joy and fulfillment.<br>
|
||||||
|
</span></li>
|
||||||
|
<li><span class="infotext"><strong>Buddhist explanation:</strong> .........................<br>
|
||||||
|
</span></li>
|
||||||
|
<li><span class="infotext"><strong>Episcopalian explanation:</strong>
|
||||||
|
Are you saying you have something against homosexuals?<br>
|
||||||
|
</span></li>
|
||||||
|
<li><span class="infotext"><strong>Christian Science explanation: </strong>There
|
||||||
|
really is no link.<br>
|
||||||
|
</span></li>
|
||||||
|
<li><span class="infotext"><strong>Atheist explanation: </strong>The only
|
||||||
|
reason you think this link exists is because you needed to invent it.<br>
|
||||||
|
</span></li>
|
||||||
|
<li><span class="infotext"><strong>Church counselor's explanation:</strong>
|
||||||
|
And what did you feel when the link would not open?</span></li>
|
||||||
|
</ul>
|
||||||
|
<p><br>
|
||||||
|
</p>
|
||||||
|
|
||||||
|
<h2 id="ietext" style="font:8pt/11pt verdana; color:black"><img
|
||||||
|
src="http://www.larknews.com/images/search.gif" align="top" height="16" width="16">
|
||||||
|
HTTP 404 - File not found - Internet Explorer <br>
|
||||||
|
</h2>
|
||||||
|
</font></td>
|
||||||
|
</tr>
|
||||||
|
</tbody>
|
||||||
|
</table>
|
||||||
|
|
||||||
|
</body>
|
||||||
|
|
||||||
|
|
||||||
|
</html>
|
Reference in New Issue
Block a user