How to get uploaded file name?

I'm trying to make upload with Jersey multipart. Everything working well. But I cannot get uploaded filename. I think it's library problem.

Here is dependencies from my pom.xml

<dependencies>
    <dependency>
        <groupId>javax</groupId>
        <artifactId>javaee-api</artifactId>
        <version>7.0</version>
    </dependency>
    <dependency>
        <groupId>org.glassfish.jersey.bundles</groupId>
        <artifactId>jaxrs-ri</artifactId>
        <version>2.21</version>
    </dependency>
    <dependency>
        <groupId>commons-net</groupId>
        <artifactId>commons-net</artifactId>
        <version>3.3</version>
    </dependency>
    <dependency>
        <groupId>org.glassfish.jersey.media</groupId>
        <artifactId>jersey-media-multipart</artifactId>
        <version>2.17</version>
    </dependency>
    <dependency>
        <groupId>com.sun.jersey</groupId>
        <artifactId>jersey-core</artifactId>
        <version>1.18.1</version>
    </dependency>
</dependencies>

My UpploadFileService

@Path("/file")
public class UploadFileService {

    @POST
    @Path("/upload")
    @Consumes(MediaType.MULTIPART_FORM_DATA)
    public Response uploadFile(
            @FormDataParam("file") InputStream uploadedInputStream,
            @FormDataParam("file1") FormDataContentDisposition fileDetail) {
        ... 
    }
}

If I change @FormDataParam("file1") to @FormDataParam("file") POST request will return Bad Request. But it's working well without it.

How can I get filename? And why @FormDataParam("file") makes service return Bad Request?

UPDATE

My html for uploading

<html>
<body>
<h1>File Upload with Jersey</h1>

<form action="http://localhost:8080/Project/api/file/upload" method="post" enctype="multipart/form-data">

  <p>
    Select a file : <input type="file" name="file" size="45" />
  </p>

  <input type="submit" value="Upload It" />
</form>

</body>
</html>

Application

@ApplicationPath("/api")
public class Application extends javax.ws.rs.core.Application {
    public Set<Class<?>> getClasses() {
        final Set<Class<?>> resources = new HashSet<>();
        resources.add(UploadFileService.class);
        resources.add(MultiPartFeature.class);
        return resources;
    }
}

Answers


You can get the uploaded file name using String fileName = fileDetail.getFileName();

Jersey 1 uses com.sun package and Jersey 2 uses org.glassfish package. Never mix them. Replace your

<dependency> 
    <groupId>com.sun.jersey</groupId> 
    <artifactId>jersey-core</artifactId> 
    <version>1.18.1</version> 
</dependency> 

With this one:

<dependency> 
    <groupId>org.glassfish.jersey.core</groupId> 
    <artifactId>jersey-server</artifactId> 
    <version>2.22.1</version> 
</dependency> 

Need Your Help

Do Something When Window Loads

javascript window onload

I am trying to open an alert box when a window loads. Here is what I have:

Adding javascript validation to existing acroform field in pdf with iText

java itext

I am trying to add javascript validation to an existing Acroform field via iText in java. I have written the following code so far, but no action is assigned to the form field. Is there something I...