Example usage for javax.servlet.http Part getSubmittedFileName

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

Introduction

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

Prototype

public String getSubmittedFileName();

Source Link

Document

Gets the file name specified by the client

Usage

From source file:org.eclipse.packagedrone.repo.channel.web.channel.ChannelController.java

@RequestMapping(value = "/channel/{channelId}/add", method = RequestMethod.POST)
public ModelAndView addPost(@PathVariable("channelId") final String channelId,
        @RequestParameter(required = false, value = "name") String name,
        final @RequestParameter("file") Part file) {
    try {/*from w w  w .j a  v  a 2 s.c o  m*/
        if (name == null || name.isEmpty()) {
            name = file.getSubmittedFileName();
        }

        final String finalName = name;

        this.channelService.accessRun(By.id(channelId), ModifiableChannel.class, channel -> {
            channel.getContext().createArtifact(file.getInputStream(), finalName, null);
        });

        return redirectDefaultView(channelId, true);
    } catch (final Exception e) {
        return CommonController.createError("Upload", "Upload failed", e);
    }
}

From source file:org.eclipse.packagedrone.repo.channel.web.channel.ChannelController.java

@RequestMapping(value = "/channel/{channelId}/drop", method = RequestMethod.POST)
public void drop(@PathVariable("channelId") final String channelId,
        @RequestParameter(required = false, value = "name") String name,
        final @RequestParameter("file") Part file, final HttpServletResponse response) throws IOException {
    response.setContentType("text/plain");

    try {/*w w w . ja  v a 2  s  . co  m*/
        if (name == null || name.isEmpty()) {
            name = file.getSubmittedFileName();
        }

        final String finalName = name;

        this.channelService.accessRun(By.id(channelId), ModifiableChannel.class, channel -> {
            channel.getContext().createArtifact(file.getInputStream(), finalName, null);
        });
    } catch (final Throwable e) {
        logger.warn("Failed to drop file", e);

        final Throwable cause = ExceptionHelper.getRootCause(e);

        if (cause instanceof VetoArtifactException) {
            response.setStatus(HttpServletResponse.SC_CONFLICT);
            response.getWriter().format("Artifact rejected! %s",
                    ((VetoArtifactException) cause).getVetoMessage().orElse("No details given"));
        } else {
            response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            response.getWriter().format("Internal error! %s", ExceptionHelper.getMessage(cause));
        }
        return;
    }

    response.setStatus(HttpServletResponse.SC_OK);
    response.getWriter().write("OK");
}

From source file:org.eclipse.packagedrone.repo.channel.web.channel.ChannelController.java

@RequestMapping(value = "/channel/{channelId}/artifact/{artifactId}/attach", method = RequestMethod.POST)
public ModelAndView attachArtifactPost(@PathVariable("channelId") final String channelId,
        @PathVariable("artifactId") final String artifactId,
        @RequestParameter(required = false, value = "name") final String name,
        final @RequestParameter("file") Part file) {
    return withChannel(channelId, ModifiableChannel.class, channel -> {

        String targetName = name;

        final Optional<ChannelArtifactInformation> parentArtifact = channel.getArtifact(artifactId);
        if (!parentArtifact.isPresent()) {
            return CommonController.createNotFound("artifact", artifactId);
        }/*from   w w w. j  ava 2s  .c  om*/

        try {
            if (targetName == null || targetName.isEmpty()) {
                targetName = file.getSubmittedFileName();
            }

            channel.getContext().createArtifact(artifactId, file.getInputStream(), targetName, null);
        } catch (final IOException e) {
            return new ModelAndView("/error/upload");
        }

        return new ModelAndView(
                "redirect:/channel/" + UrlEscapers.urlPathSegmentEscaper().escape(channelId) + "/view");
    });
}

From source file:mx.edu.ittepic.AEEcommerce.ejbs.OperationCommerce.java

public String updateUsers(String userid, String username, String password, String phone, String neigborhood,
        String zipcode, String city, String country, String state, String region, String street, String email,
        String streetnumber, Part file, String cellphone, String companyid, String roleid, String gender) {
    Message m = new Message();
    GsonBuilder builder = new GsonBuilder();
    Gson gson = builder.create();//w  w w. j a va2 s.  c  o  m
    try {
        Users r = new Users();
        r = entity.find(Users.class, Integer.parseInt(userid));
        if (r == null) {
            m.setCode(404);
            m.setMsg("No se encontro");
            m.setDetail(":(");
        } else {
            r.setUsername(username);
            r.setPassword(password);
            r.setPhone(phone);
            r.setNeigborhood(neigborhood);
            r.setZipcode(zipcode);
            r.setCity(city);
            r.setCountry(country);
            r.setState(state);
            r.setRegion(region);
            r.setStreet(street);
            r.setEmail(email);
            r.setStreetnumber(streetnumber);

            r.setCellphone(cellphone);
            Company companyid_ = entity.find(Company.class, Integer.parseInt(companyid));
            Role roleid_ = entity.find(Role.class, Integer.parseInt(roleid));
            r.setCompanyid(companyid_);
            r.setRoleid(roleid_);
            r.setGender(gender.charAt(0));

            String nomArch = Paths.get(file.getSubmittedFileName()).getFileName().toString();
            if (!nomArch.equals("")) {
                PreExamenProduct updateU = new PreExamenProduct();
                r.setPhoto(updateU.updateImagenU(file, r.getUserid()));
            } else {

            }
            entity.merge(r);

            m.setCode(200);
            m.setMsg("Todo bien");
            m.setDetail("OK");
        }
        return gson.toJson(m);
    } catch (IllegalArgumentException e) {
        m.setCode(404);
        m.setMsg(e.getMessage());
        m.setDetail("Error");
        return gson.toJson(m);
    } catch (TransactionRequiredException ex) {
        m.setCode(404);
        m.setMsg(ex.getMessage());
        m.setDetail("Error");
        return gson.toJson(m);
    }

}

From source file:org.apache.servicecomb.demo.jaxrs.server.CodeFirstJaxrs.java

@Path("/upload1")
@POST/*from  ww w.  j  a  v a2s  .  c om*/
@Produces(MediaType.TEXT_PLAIN)
public String fileUpload1(@FormParam("file1") Part file1, @FormParam("file2") Part file2) throws IOException {
    if (file1 == null || file2 == null) {
        return "null file";
    }
    try (InputStream is1 = file1.getInputStream(); InputStream is2 = file2.getInputStream()) {
        String content1 = IOUtils.toString(is1);
        String content2 = IOUtils.toString(is2);
        return String.format("%s:%s:%s\n" + "%s:%s:%s", file1.getSubmittedFileName(), file1.getContentType(),
                content1, file2.getSubmittedFileName(), file2.getContentType(), content2);
    }
}

From source file:org.apache.servicecomb.demo.jaxrs.server.CodeFirstJaxrs.java

@Path("/upload2")
@POST/*from www  .  j  av  a2  s  . com*/
@Produces(MediaType.TEXT_PLAIN)
public String fileUpload2(@FormParam("file1") Part file1, @FormParam("message") String message)
        throws IOException {
    try (InputStream is1 = file1.getInputStream()) {
        String content1 = IOUtils.toString(is1);
        return String.format("%s:%s:%s:%s", file1.getSubmittedFileName(), file1.getContentType(), content1,
                message);
    }
}

From source file:org.apache.servicecomb.demo.springmvc.server.CodeFirstSpringmvc.java

private String _fileUpload(MultipartFile file1, Part file2) {
    try (InputStream is1 = file1.getInputStream(); InputStream is2 = file2.getInputStream()) {
        String content1 = IOUtils.toString(is1);
        String content2 = IOUtils.toString(is2);
        return String.format("%s:%s:%s\n" + "%s:%s:%s", file1.getOriginalFilename(), file1.getContentType(),
                content1, file2.getSubmittedFileName(), file2.getContentType(), content2);
    } catch (IOException e) {
        throw new IllegalArgumentException(e);
    }/* w w  w.jav a  2s . co m*/
}

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

@Test
public void prepareSendPartHeader_update(@Mocked Part part) {
    new Expectations() {
        {//from  w ww.  j  ava 2 s  .  c o m
            part.getContentType();
            result = "type";
            part.getSubmittedFileName();
            result = "     ";
        }
    };
    DownloadUtils.prepareDownloadHeader(response, part);

    Assert.assertTrue(serverResponse.isChunked());
    Assert.assertEquals("type", response.getHeader(HttpHeaders.CONTENT_TYPE));
    Assert.assertEquals(
            "attachment;filename=%E6%B5%8B%20%20%20%20%20%E8%AF%95;filename*=utf-8''%E6%B5%8B%20%20%20%20%20%E8%AF%95",
            response.getHeader(HttpHeaders.CONTENT_DISPOSITION));
}

From source file:org.apache.sling.servlets.post.impl.operations.StreamedUploadOperation.java

/**
 * Is the part a form field ?/*from w w w. j  av  a 2  s . c o m*/
 * @param part
 * @return
 */
private boolean isFormField(Part part) {
    return (part.getSubmittedFileName() == null);
}

From source file:org.apache.sling.servlets.post.impl.operations.StreamedUploadOperation.java

/**
 * Get the upload file name from the part.
 * @param part// w  w w.  ja  v a 2  s.c o  m
 * @return
 */
private String getUploadName(Part part) {
    // only return non null if the submitted file name is non null.
    // the Sling API states that if the field name is '*' then the submitting file name is used,
    // otherwise the field name is used.
    String name = part.getName();
    String fileName = part.getSubmittedFileName();
    if ("*".equals(name)) {
        name = fileName;
    }
    // strip of possible path (some browsers include the entire path)
    name = name.substring(name.lastIndexOf('/') + 1);
    name = name.substring(name.lastIndexOf('\\') + 1);
    return Text.escapeIllegalJcrChars(name);
}