Skip to content

Instantly share code, notes, and snippets.

@danielkec
Last active December 30, 2015 00:09
Show Gist options
  • Save danielkec/7748053 to your computer and use it in GitHub Desktop.
Save danielkec/7748053 to your computer and use it in GitHub Desktop.
package cz.syntea.dn.web2.server;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import cz.syntea.dn.jul.Logger;
/**
* DNDownload
*
* Stahovani souboru se jnmenem z parametru fileName naplneneho textem z parametru content, a typu z parametru mimeType<br/>
* priklad: ../DNDoenload?fileName=readme&content=Ahoj%Svete&mimeType=text/html&
* @author Daniel Kec <daniel.kec at syntea.cz>
* @since Aug 29, 2011
* @version 1.0
* @class cz.syntea.dn.web2.server.DNDownload
*/
public class DNDownload extends HttpServlet {
private static Logger LOG = Logger.getLogger(DNDownload.class);
private static final long serialVersionUID = 1L;
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String fileName = getParam(request, "fileName");
String content = getParam(request, "content");
String mimeType = getParam(request, "mimeType");
doDownload(request, response,
fileName,
content,
mimeType);
}
/**
* Presmeruje se automaticky na doPost
*/
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);
}
private void doDownload(HttpServletRequest req, HttpServletResponse resp,
String fileName, String content, String mimeType)
throws IOException {
LOG.entering(DNDownload.class.getCanonicalName(), "doDownload");
LOG.config("DNDownload: Priprava ke stazeni souboru "+fileName+" typu "+mimeType);
InputStream istr = null;
OutputStream ostr = null;
resp.setContentType(mimeType);
resp.setHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\";");
try {
// tohle kodovani je tu kvuli chybe v tomcatu ktery moc neumi s cestinou, zda se ze ve websphere je taky, nahoda?
istr = new ByteArrayInputStream(content.getBytes("ISO-8859-1"));
ostr = resp.getOutputStream();
int curByte = -1;
while ((curByte = istr.read()) != -1) {
ostr.write(curByte);
}
ostr.flush();
} catch (Exception ex) {
LOG.severe("Chyba pri odesilani souboru "+fileName+" typu "+mimeType+": ", ex);
} finally {
try {
if (istr != null) {
istr.close();
}
if (ostr != null) {
ostr.close();
}
} catch (Exception ex) {
LOG.severe("Chyba pri uzavirani streamu: ", ex);
}
}
try {
resp.flushBuffer();
} catch (Exception ex) {
LOG.severe("Chyba pri flushovani odpovedi servletu: ", ex);
}
LOG.config("DNDownload: Soubor "+fileName+" typu "+mimeType+" byl odeslan prohlizeci.");
LOG.exiting(DNDownload.class.getCanonicalName(), "doDownload");
}
/**
* Vraci pozadovany parametr z requestu podle zadaneho jmena
* @param request ze ktereho chceme ziskat parametr
* @param name jmeno parametru
* @return hodnota paramtru
*/
private String getParam(HttpServletRequest request, String name) {
String result = request.getParameter(name);
if (result != null) {
return result.trim();
}
return "";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment