List of usage examples for javax.servlet ServletInputStream read
public abstract int read() throws IOException;
From source file:com.intuit.autumn.web.InputStreamHttpServletRequestWrapperTest.java
@Test public void testGetInputStreamRead() throws Exception { PowerMockito.whenNew(ByteArrayInputStream.class).withArguments(any()).thenReturn(byteArrayInputStream); when(byteArrayInputStream.read()).thenReturn(5); ServletInputStream is = inputStreamHttpServletRequestWrapper.getInputStream(); assertThat(is.read(), is(5)); }
From source file:org.zalando.logbook.servlet.example.ExampleController.java
@RequestMapping(value = "/read-byte", produces = MediaType.TEXT_PLAIN_VALUE) public void readByte(final HttpServletRequest request, final HttpServletResponse response) throws IOException { final ServletInputStream input = request.getInputStream(); final ServletOutputStream output = response.getOutputStream(); while (true) { final int read = input.read(); if (read == -1) { break; }//from w w w . ja va 2 s . com output.write(read); } }
From source file:org.sakaiproject.myshowcase.tool.MyShowcaseFileUploadController.java
protected ModelAndView onSubmit(HttpServletRequest request, HttpServletResponse response, Object command, org.springframework.validation.BindException errors) throws ServletException, IOException { System.out.println("MyShowcaseFileUploadController onSubmit Start"); // System.out.println("Content type: " + request.getContentType()) ; // System.out.println("Content length: " + request.getContentLength()) ; response.setContentType("text/html"); try {/*from www . j a v a2s. c o m*/ // String contentType = request.getContentType(); // if ((contentType != null) && (contentType.indexOf("multipart/form-data") >= 0)) { System.out.println("We have a file"); // System.out.println("Request Content Length: " + request.getContentLength()) ; ServletInputStream reader = request.getInputStream(); while (reader.read() != -1) { System.out.println("Loop"); } // } } catch (Exception e) { System.out.println("It didn't work"); System.out.println(e.toString()); } return null; /* DataInputStream in = new DataInputStream(request.getInputStream()); int formDataLength = request.getContentLength(); System.out.println("FormDataLength: " + formDataLength) ; int totalBytes = 0 ; int maxBlockSize = 16577856 ; //maxBlockSize = 20 ; if (formDataLength < maxBlockSize) { // Do it in one hit byte dataBytes[] = new byte[formDataLength]; int byteRead = 0; int totalBytesRead = 0; int c = 0 ; while ((totalBytesRead < formDataLength) && (c < 10)){ byteRead = in.read(dataBytes, totalBytesRead,formDataLength); totalBytesRead += byteRead; System.out.println("ByteRead : " + byteRead) ; System.out.println("TotalBytes : " + totalBytesRead) ; c++ ; } String file = new String(dataBytes) ; String saveFile = file.substring(file.indexOf("filename=\"") + 10); saveFile = saveFile.substring(0, saveFile.indexOf("\n")); saveFile = saveFile.substring(saveFile.lastIndexOf("\\") + 1,saveFile.indexOf("\"")); int lastIndex = contentType.lastIndexOf("="); String boundary = contentType.substring(lastIndex + 1,contentType.length()); int pos; pos = file.indexOf("filename=\""); pos = file.indexOf("\n", pos) + 1; pos = file.indexOf("\n", pos) + 1; pos = file.indexOf("\n", pos) + 1; int boundaryLocation = file.indexOf(boundary, pos) - 4; int startPos = ((file.substring(0, pos)).getBytes()).length; int endPos = ((file.substring(0, boundaryLocation)).getBytes()).length; saveFile = "C:\\temp\\" + saveFile ; System.out.println(saveFile) ; FileOutputStream fileOut = new FileOutputStream(saveFile); fileOut.write(dataBytes, startPos, (endPos - startPos)); fileOut.flush(); fileOut.close(); } else { byte buf[] = new byte[2048]; System.out.println("Do it in chunks") ; int lastIndex = contentType.lastIndexOf("="); String boundary = contentType.substring(lastIndex + 1,contentType.length()); System.out.print("Last Index >" + lastIndex + "<") ; // out.print("Boundary >" + boundary + "<") ; BufferedInputStream bin = new BufferedInputStream(in); int totalBuffers = 0 ; System.out.print("Start") ; String saveFile ; //Do initial header bin.read(buf) ; System.out.println( " Buffer 0 "); String file = new String(buf) ; saveFile = file.substring(file.indexOf("filename=\"") + 10); saveFile = saveFile.substring(0, saveFile.indexOf("\n")); saveFile = saveFile.substring(saveFile.lastIndexOf("\\") + 1,saveFile.indexOf("\"")); lastIndex = contentType.lastIndexOf("="); boundary = contentType.substring(lastIndex + 1,contentType.length()); int pos; pos = file.indexOf("filename=\""); pos = file.indexOf("\n", pos) + 1; pos = file.indexOf("\n", pos) + 1; pos = file.indexOf("\n", pos) + 1; int boundaryLocation = file.indexOf(boundary, pos) - 4; int startPos = ((file.substring(0, pos)).getBytes()).length; int endPos = ((file.substring(0, boundaryLocation)).getBytes()).length; saveFile = "C:\\temp\\" + saveFile ; System.out.print("Save File >" + saveFile) ; FileOutputStream fileOut = new FileOutputStream(saveFile); fileOut.write(buf) ; while ((bin.read(buf)) != -1) { fileOut.write(buf); totalBuffers++ ; } System.out.print("End") ; System.out.print("Totalbuffers : " + totalBuffers) ; bin.close(); fileOut.flush(); fileOut.close(); } } } catch (Exception e) { System.out.println("It didn't work") ; System.out.println(e.toString()) ; } System.out.println("Bye bye") ; return null ; */ }
From source file:org.apache.solr.servlet.SolrDispatchFilter.java
private void consumeInputFully(HttpServletRequest req) { try {/*from w w w . j av a2 s . c om*/ ServletInputStream is = req.getInputStream(); while (!is.isFinished() && is.read() != -1) { } } catch (IOException e) { log.info("Could not consume full client request", e); } }
From source file:org.atomserver.server.servlet.BlockingFilter.java
private ServletRequest wrapServletRequest(ServletRequest servletRequest) throws IOException { final ServletInputStream originalInputStream = servletRequest.getInputStream(); final int maxBytes = settings.getMaxContentLength(); final ServletInputStream inputStream = new ServletInputStream() { int bytesRead = 0; public int read() throws IOException { if (bytesRead++ > maxBytes) { throw new TooMuchDataException("Content length exceeds the maximum length allowed."); }/*from w w w . ja v a2 s. c o m*/ return originalInputStream.read(); } }; // wrap the original request, returning the wrapped input stream instead return new HttpServletRequestWrapper((HttpServletRequest) servletRequest) { public ServletInputStream getInputStream() throws IOException { return inputStream; } }; }
From source file:com.sitewhere.web.rest.controllers.AssignmentsController.java
/** * Adds data to an existing device stream. * /*from w ww . ja v a2 s . c om*/ * @param token * @param streamId * @param sequenceNumber * @param svtRequest * @param svtResponse * @throws SiteWhereException */ @RequestMapping(value = "/{token}/streams/{streamId:.+}", method = RequestMethod.POST) @ResponseBody @ApiOperation(value = "Add data to device assignment data stream") @Secured({ SiteWhereRoles.REST }) @Documented public void addDeviceStreamData( @ApiParam(value = "Assignment token", required = true) @PathVariable String token, @ApiParam(value = "Stream Id", required = true) @PathVariable String streamId, @ApiParam(value = "Sequence Number", required = false) @RequestParam(required = false) Long sequenceNumber, HttpServletRequest servletRequest, HttpServletResponse svtResponse) throws SiteWhereException { Tracer.start(TracerCategory.RestApiCall, "addDeviceStreamData", LOGGER); try { ServletInputStream inData = servletRequest.getInputStream(); ByteArrayOutputStream byteData = new ByteArrayOutputStream(); int data; while ((data = inData.read()) != -1) { byteData.write(data); } byte[] payload = byteData.toByteArray(); DeviceStreamDataCreateRequest request = new DeviceStreamDataCreateRequest(); request.setStreamId(streamId); request.setSequenceNumber(sequenceNumber); request.setEventDate(new Date()); request.setUpdateState(false); request.setData(payload); SiteWhere.getServer().getDeviceEventManagement(getTenant(servletRequest)).addDeviceStreamData(token, request); svtResponse.setStatus(HttpServletResponse.SC_CREATED); } catch (SiteWhereSystemException e) { if (e.getCode() == ErrorCode.InvalidStreamId) { svtResponse.setStatus(HttpServletResponse.SC_NOT_FOUND); } else { LOGGER.error("Unhandled SiteWhere exception.", e); svtResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR); } } catch (IOException e) { LOGGER.error(e); svtResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR); } finally { Tracer.stop(LOGGER); } }