List of usage examples for javax.servlet ServletOutputStream write
public void write(byte b[], int off, int len) throws IOException
len
bytes from the specified byte array starting at offset off
to this output stream. From source file:cz.incad.Kramerius.audio.AudioHttpRequestForwarder.java
private void forwardData(InputStream input, ServletOutputStream output) { try {/*from w ww . j a v a 2 s . com*/ byte[] buffer = new byte[BUFFER_SIZE]; int bytesForwarded; while ((bytesForwarded = input.read(buffer)) != -1) { output.write(buffer, 0, bytesForwarded); } } catch (IOException ex) { if (ex.getCause() != null && ex.getCause() instanceof SocketException && (ex.getCause().getMessage().equals(CONNECTION_RESET) || ex.getCause().getMessage().equals(BROKEN_PIPE))) { LOGGER.warning("Connection reset probably by client (or by repository)"); } else { LOGGER.log(Level.SEVERE, null, ex); } } finally { try { LOGGER.fine("closing connection to repository"); input.close(); //output stream should not be closed here because it is closed by container. //If closed here, for example filters won't be able to write to stream anymore } catch (IOException ex1) { LOGGER.log(Level.SEVERE, "Failed to close connection to repository", ex1); } } }
From source file:UploadDownloadFile.java
@Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { processRequest(request, response);/*w ww. j a v a 2s. c om*/ System.out.println("--> doGet -->"); String filePath; String fileName = request.getParameter("fileName"); if (fileName == null || fileName.equals("")) { throw new ServletException("File Name can't be null or empty"); } final String clientip = request.getRemoteAddr().replace(":", "_"); filePath = "/Users/" + currentuser + "/GlassFish_Server/glassfish/domains/domain1/tmpfiles/Uploaded/" + clientip + "/"; File file = new File(filePath + File.separator + fileName); System.out.println("download file = " + file); if (!file.exists()) { throw new ServletException("File doesn't exists on server."); } System.out.println("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", "attachment; filename=\"" + fileName + "\""); ServletOutputStream os = response.getOutputStream(); byte[] bufferData = new byte[1024]; int read = 0; while ((read = fis.read(bufferData)) != -1) { os.write(bufferData, 0, read); } os.flush(); os.close(); fis.close(); System.out.println("File downloaded at client successfully"); }
From source file:com.arcadian.loginservlet.StudentAssignmentServlet.java
/** * Handles the HTTP/*from w w w . ja v a2s. c o m*/ * <code>GET</code> method. * * @param request servlet request * @param response servlet response * @throws ServletException if a servlet-specific error occurs * @throws IOException if an I/O error occurs */ @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { if (request.getParameter("filename") != null) { String fileName = request.getParameter("filename"); File file = new File(request.getServletContext().getAttribute("FILES_DIR") + File.separator + fileName); System.out.println("File location on server::" + file.getAbsolutePath()); ServletContext ctx = getServletContext(); InputStream fis = new FileInputStream(file); String mimeType = ctx.getMimeType(file.getAbsolutePath()); System.out.println("mime type=" + mimeType); response.setContentType(mimeType != null ? mimeType : "application/octet-stream"); response.setContentLength((int) file.length()); response.setHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\""); ServletOutputStream os = response.getOutputStream(); byte[] bufferData = new byte[102400]; int read = 0; while ((read = fis.read(bufferData)) != -1) { os.write(bufferData, 0, read); } os.flush(); os.close(); fis.close(); System.out.println("File downloaded at client successfully"); } processRequest(request, response); }
From source file:org.flowable.app.rest.idm.IdmProfileResource.java
@RequestMapping(value = "/profile-picture", method = RequestMethod.GET) public void getProfilePicture(HttpServletResponse response) { try {/*from w ww . j av a2 s . c o m*/ Pair<String, InputStream> picture = profileService.getProfilePicture(); if (picture == null) { throw new NotFoundException(); } response.setContentType(picture.getLeft()); ServletOutputStream servletOutputStream = response.getOutputStream(); byte[] buffer = new byte[32384]; while (true) { int count = picture.getRight().read(buffer); if (count == -1) break; servletOutputStream.write(buffer, 0, count); } // Flush and close stream servletOutputStream.flush(); servletOutputStream.close(); } catch (Exception e) { throw new InternalServerErrorException("Could not get profile picture", e); } }
From source file:com.arcadian.loginservlet.LecturesServlet.java
/** * Handles the HTTP// w ww. ja va 2 s . com * <code>GET</code> method. * * @param request servlet request * @param response servlet response * @throws ServletException if a servlet-specific error occurs * @throws IOException if an I/O error occurs */ @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { if (request.getParameter("filename") != null) { String fileName = request.getParameter("filename"); File file = new File(request.getServletContext().getAttribute("FILES_DIR") + File.separator + fileName); System.out.println("File location on server::" + file.getAbsolutePath()); ServletContext ctx = getServletContext(); InputStream fis = new FileInputStream(file); String mimeType = ctx.getMimeType(file.getAbsolutePath()); System.out.println("mime type=" + mimeType); response.setContentType(mimeType != null ? mimeType : "application/octet-stream"); response.setContentLength((int) file.length()); response.setHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\""); ServletOutputStream os = response.getOutputStream(); byte[] bufferData = new byte[102400]; int read = 0; while ((read = fis.read(bufferData)) != -1) { os.write(bufferData, 0, read); } os.flush(); os.close(); fis.close(); System.out.println("File downloaded at client successfully"); } processRequest(request, response); }
From source file:com.exedio.cope.live.MediaServlet.java
void doRequest(final HttpServletRequest request, final HttpServletResponse response, final Anchor anchor) { final String featureID = request.getParameter(FEATURE); if (featureID == null) throw new NullPointerException(); final Media feature = (Media) model.getFeature(featureID); if (feature == null) throw new NullPointerException(featureID); final String itemID = request.getParameter(ITEM); if (itemID == null) throw new NullPointerException(); final Item item; try {//from w ww . j a v a2 s. co m startTransaction("media(" + featureID + ',' + itemID + ')'); item = model.getItem(itemID); model.commit(); } catch (final NoSuchIDException e) { throw new RuntimeException(e); } finally { model.rollbackIfNotCommitted(); } final FileItem fi = anchor.getModification(feature, item); if (fi == null) throw new NullPointerException(featureID + '-' + itemID); response.setContentType(fi.getContentType()); response.setContentLength((int) fi.getSize()); InputStream in = null; ServletOutputStream out = null; try { in = fi.getInputStream(); out = response.getOutputStream(); final byte[] b = new byte[20 * 1024]; for (int len = in.read(b); len >= 0; len = in.read(b)) out.write(b, 0, len); } catch (final IOException e) { throw new RuntimeException(e); } finally { if (in != null) { try { in.close(); } catch (final IOException e) { e.printStackTrace(); } } if (out != null) { try { out.close(); } catch (final IOException e) { e.printStackTrace(); } } } }
From source file:br.com.hslife.orcamento.controller.ArquivoController.java
public void baixarArquivo() { HttpServletResponse response = (HttpServletResponse) FacesContext.getCurrentInstance().getExternalContext() .getResponse();// ww w .j a v a 2 s.c om try { response.setContentType(entity.getContentType()); response.setHeader("Content-Disposition", "attachment; filename=" + entity.getNomeArquivo()); response.setContentLength(entity.getDados().length); ServletOutputStream output = response.getOutputStream(); output.write(entity.getDados(), 0, entity.getDados().length); FacesContext.getCurrentInstance().responseComplete(); } catch (Exception e) { errorMessage(e.getMessage()); } }
From source file:pl.psnc.synat.wrdz.zmkd.plan.MigrationPlansBean.java
/** * XML migration plan file download action. Sets request response as XML stream. * // w w w .ja va 2 s. com * @param plan * plan from which XML file is taken */ public void downloadXML(MigrationPlan plan) { HttpServletResponse response = (HttpServletResponse) FacesContext.getCurrentInstance().getExternalContext() .getResponse(); response.setContentType("text/xml"); response.setHeader("Content-Disposition", "attachment; filename=migration_plan_" + plan.getId() + ".xml"); response.setContentLength(plan.getXmlFile().length()); try { ServletOutputStream ouputStream = response.getOutputStream(); ouputStream.write(plan.getXmlFile().getBytes(), 0, plan.getXmlFile().getBytes().length); ouputStream.flush(); ouputStream.close(); } catch (Exception ex) { LOGGER.error(ex); } }
From source file:info.magnolia.rendering.engine.RenderingFilter.java
/** * Send data as is./* w w w. j a va2 s. c o m*/ * @param is Input stream for the resource * @param response HttpServletResponse as received by the service method * @throws IOException standard servlet exception */ private void sendUnCompressed(InputStream is, HttpServletResponse response) throws IOException { ServletOutputStream os = response.getOutputStream(); byte[] buffer = new byte[8192]; int read; while ((read = is.read(buffer)) > 0) { os.write(buffer, 0, read); } os.flush(); IOUtils.closeQuietly(os); }
From source file:shapeways.api.robocreator.RoboCreatorWeb.java
private void issueError(String reason, HttpServletResponse resp) throws IOException { HashMap<String, Object> result = new HashMap<String, Object>(); Gson gson = new Gson(); result.put("result", "failure"); result.put("reason", reason); String json = gson.toJson(result); resp.setContentLength(json.length()); resp.setStatus(HttpServletResponse.SC_BAD_REQUEST); ServletOutputStream sos = resp.getOutputStream(); byte[] ba = json.getBytes(); sos.write(ba, 0, ba.length); resp.flushBuffer();//from ww w .ja v a2 s.c o m }