Example usage for javax.servlet.http Part getInputStream

List of usage examples for javax.servlet.http Part getInputStream

Introduction

In this page you can find the example usage for javax.servlet.http Part getInputStream.

Prototype

public InputStream getInputStream() throws IOException;

Source Link

Document

Gets the content of this part as an InputStream

Usage

From source file:net.voidfunction.rm.master.AdminServlet.java

protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    response.setHeader("Date", HTTPUtils.getServerTime(0));
    response.setContentType("text/html");
    response.setStatus(HttpServletResponse.SC_OK);

    Part uploadFile = request.getPart("uploadfile");
    if (uploadFile == null)
        throw new ServletException("Uploaded file is null.");

    // Get some info about the file
    String filename = getFilename(uploadFile);
    String contentType = uploadFile.getContentType();
    long size = uploadFile.getSize();
    byte[] hash = FileUtils.sha256Hash(uploadFile.getInputStream());

    // Create a new file object, add it to the database, and store data
    RMFile newFile = new RMFile(filename, contentType, size, hash);
    node.getFileRepository().addFile(newFile, uploadFile.getInputStream());

    // Output data for interested parties
    Template tpl = new Template(new File(templateDir + "uploadsuccess.tpl"));
    tpl.assign("FILENAME", filename);
    tpl.assign("SIZE", String.valueOf(size));
    tpl.assign("TYPE", contentType);
    tpl.assign("HASH", Hex.encodeHexString(hash));
    tpl.assign("FILEID", newFile.getId());
    tpl.parse("main");
    response.getWriter().print(tpl.out());

    // Delete temp file
    uploadFile.delete();//from   w  w w. j a v a  2s  . co m

    // Log
    node.getLog().info("New file added (via web): " + newFile.getId() + " (" + newFile.getName() + ")");
}

From source file:com.ibm.liberty.starter.api.v1.LibertyFileUploader.java

protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    String tech = request.getParameter(PARAMETER_TECH);
    String workspaceId = request.getParameter(PARAMETER_WORKSPACE); //specify the unique workspace directory to upload the file(s) to.   
    Collection<Part> filePartCollection = request.getParts();

    String serverHostPort = request.getRequestURL().toString().replace(request.getRequestURI(), "");
    int schemeLength = request.getScheme().toString().length();
    String internalHostPort = "http" + serverHostPort.substring(schemeLength);
    log.log(Level.FINER, "serverHostPort : " + serverHostPort);
    final ServiceConnector serviceConnector = new ServiceConnector(serverHostPort, internalHostPort);
    HashMap<Part, String> fileNames = new HashMap<Part, String>();
    if (!isValidRequest(request, response, tech, workspaceId, filePartCollection, serviceConnector,
            fileNames)) {//from   w w w .  ja v a  2 s.c  o  m
        return;
    }

    Service techService = serviceConnector.getServiceObjectFromId(tech);
    String techDirPath = StarterUtil.getWorkspaceDir(workspaceId) + "/" + techService.getId();
    File techDir = new File(techDirPath);
    if (techDir.exists() && techDir.isDirectory()
            && "true".equalsIgnoreCase(request.getParameter(PARAMETER_CLEANUP))) {
        FileUtils.cleanDirectory(techDir);
        log.log(Level.FINER, "Cleaned up tech workspace directory : " + techDirPath);
    }

    for (Part filePart : filePartCollection) {
        if (!techDir.exists()) {
            FileUtils.forceMkdir(techDir);
            log.log(Level.FINER, "Created tech directory :" + techDirPath);
        }

        String filePath = techDirPath + "/" + fileNames.get(filePart);
        log.log(Level.FINER, "File path : " + filePath);
        File uploadedFile = new File(filePath);

        Files.copy(filePart.getInputStream(), uploadedFile.toPath(), StandardCopyOption.REPLACE_EXISTING);
        log.log(Level.FINE, "Copied file to " + filePath);
    }

    if ("true".equalsIgnoreCase(request.getParameter(PARAMETER_PROCESS))) {
        // Process uploaded file(s)
        String processResult = serviceConnector.processUploadedFiles(techService, techDirPath);
        if (!processResult.equalsIgnoreCase("success")) {
            log.log(Level.INFO,
                    "Error processing the files uploaded to " + techDirPath + " : Result=" + processResult);
            response.sendError(500, processResult);
            return;
        }
        log.log(Level.FINE, "Processed the files uploaded to " + techDirPath);
    }

    response.setContentType("text/html");
    PrintWriter out = response.getWriter();
    out.println("success");
    out.close();
}

From source file:org.apache.servicecomb.foundation.vertx.http.TestVertxServerResponseToHttpServletResponse.java

@Test
public void sendPart_openInputStreamFailed(@Mocked Part part)
        throws IOException, InterruptedException, ExecutionException {
    IOException ioException = new IOException("forbid open stream");
    new Expectations() {
        {//  w w  w.ja  v  a2  s.com
            part.getInputStream();
            result = ioException;
        }
    };

    CompletableFuture<Void> future = response.sendPart(part);

    expectedException.expect(ExecutionException.class);
    expectedException.expectCause(Matchers.sameInstance(ioException));

    future.get();
}

From source file:z.tool.web.servlet.AbstractServlet.java

protected File getPartFile(HttpServletRequest request, String key) throws ServletError {
    Part part = getPart(request, key);
    if (null == part) {
        return null;
    }// www  .j a v a 2s. co  m

    FileOutputStream fos = null;
    try {
        File tmp = File.createTempFile("servlet_part", "tmp");
        fos = new FileOutputStream(tmp);
        IOUtils.copy(part.getInputStream(), fos);
        return tmp;
    } catch (IOException e) {
        throw new ServletError(e);
    } finally {
        IOUtils.closeQuietly(fos);
    }
}

From source file:org.apache.servicecomb.foundation.vertx.http.TestVertxServerResponseToHttpServletResponse.java

@Test
public void sendPart_inputStreamBreak(@Mocked Part part, @Mocked InputStream inputStream)
        throws IOException, InterruptedException, ExecutionException {
    IOException ioException = new IOException("forbid read");
    new Expectations() {
        {/*  ww  w  .j a  v  a2 s  . c  o  m*/
            part.getInputStream();
            result = inputStream;
            inputStream.read((byte[]) any);
            result = ioException;
        }
    };

    CompletableFuture<Void> future = response.sendPart(part);

    expectedException.expect(ExecutionException.class);
    expectedException.expectCause(Matchers.sameInstance(ioException));

    future.get();
}

From source file:com.stitchgalaxy.sg_manager_web.ProductController.java

@RequestMapping(value = UrlConstants.URL_PRODUCT_UPLOAD_IMAGE, method = RequestMethod.POST)
public String uploadImage(Model model, @RequestParam(value = "product") Long productId,
        @RequestParam("file") Part file) throws DomainDataServiceException, IOException {
    CommandUploadProductFile command = new CommandUploadProductFile();
    command.setProductId(productId);// w  w w .j ava 2 s  . c om
    command.setFileType(FileType.IMAGE);
    InputStream filecontent = file.getInputStream();
    command.setFileContent(filecontent);
    domainDataService.uploadProductFile(command);
    return "redirect:" + UrlConstants.URL_PRODUCT_VIEW + "?product=" + productId;
}

From source file:com.stitchgalaxy.sg_manager_web.ProductController.java

@RequestMapping(value = UrlConstants.URL_PRODUCT_UPLOAD_DESIGN, method = RequestMethod.POST)
public String uploadDesign(Model model, @RequestParam(value = "product") Long productId,
        @RequestParam("file") Part file) throws DomainDataServiceException, IOException {
    CommandUploadProductFile command = new CommandUploadProductFile();
    command.setProductId(productId);//from ww  w  .j  a va 2  s.c o  m
    command.setFileType(FileType.DESIGN);
    InputStream filecontent = file.getInputStream();
    command.setFileContent(filecontent);
    domainDataService.uploadProductFile(command);
    return "redirect:" + UrlConstants.URL_PRODUCT_VIEW + "?product=" + productId;
}

From source file:com.stitchgalaxy.sg_manager_web.ProductController.java

@RequestMapping(value = UrlConstants.URL_PRODUCT_UPLOAD_THUMBNAIL, method = RequestMethod.POST)
public String uploadThumbnail(Model model, @RequestParam(value = "product") Long productId,
        @RequestParam("file") Part file) throws DomainDataServiceException, IOException {
    CommandUploadProductFile command = new CommandUploadProductFile();
    command.setProductId(productId);//from   w  w  w .  j ava  2  s .c om
    command.setFileType(FileType.THUMBNAIL);
    InputStream filecontent = file.getInputStream();
    command.setFileContent(filecontent);
    domainDataService.uploadProductFile(command);
    return "redirect:" + UrlConstants.URL_PRODUCT_VIEW + "?product=" + productId;
}

From source file:com.stitchgalaxy.sg_manager_web.ProductController.java

@RequestMapping(value = UrlConstants.URL_PRODUCT_UPLOAD_PROTOTYPE, method = RequestMethod.POST)
public String uploadPrototype(Model model, @RequestParam(value = "product") Long productId,
        @RequestParam("file") Part file) throws DomainDataServiceException, IOException {
    CommandUploadProductFile command = new CommandUploadProductFile();
    command.setProductId(productId);//  ww w .  ja  v  a 2s. c om
    command.setFileType(FileType.PROTOTYPE);
    InputStream filecontent = file.getInputStream();
    command.setFileContent(filecontent);
    domainDataService.uploadProductFile(command);
    return "redirect:" + UrlConstants.URL_PRODUCT_VIEW + "?product=" + productId;
}

From source file:com.stitchgalaxy.sg_manager_web.ProductController.java

@RequestMapping(value = UrlConstants.URL_PRODUCT_UPLOAD_COMPLETE_PRODUCT, method = RequestMethod.POST)
public String uploadCompleteProduct(Model model, @RequestParam(value = "product") Long productId,
        @RequestParam("file") Part file) throws DomainDataServiceException, IOException {
    CommandUploadProductFile command = new CommandUploadProductFile();
    command.setProductId(productId);//from  ww w  .  j  a  va2  s .c o m
    command.setFileType(FileType.COMPLETE_PRODUCT);
    InputStream filecontent = file.getInputStream();
    command.setFileContent(filecontent);
    domainDataService.uploadProductFile(command);
    return "redirect:" + UrlConstants.URL_PRODUCT_VIEW + "?product=" + productId;
}