2

Is it possible to add the file extension into the HTTP header?

I have developed a program for sending a file using an API call. The file will not be restricted to .doc or .pdf, it can be with .exe or .zip extension as well.

The file is successfully uploaded to the server but the file extension seems not correct, it's showing the file type as data, while I'm expecting another file type.

Here down the sample code sources for the upload part:

HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost("http://ServerName:Port/Anything/data");  //remote API URL 
String filepath = "C://User//test//";
String file1 = filepath.concat(file);
System.out.println("Sending the file");
FileBody bin = new FileBody(new File(file1));  // take the file from directory 
HttpEntity reqEntity = MultipartEntityBuilder.create()
        .addPart("display_name", (ContentBody) bin)
        .build();
post.addHeader("filename", file1);  
post.setEntity(reqEntity);  // send via post method



Will the FileBody read the file extension as well?

tmarwen
  • 15,750
  • 5
  • 43
  • 62
attack
  • 103
  • 1
  • 11

3 Answers3

1

Extract the Content-Disposition header from the headers list using (HttpServletRequest) request.getHeader("Content-Disposition"). This will have the filename present in last as Content-Disposition: form-data; name="uploadedfile"; filename="file.ext".

From here you can extract the file type.

You can do it in way also:

String fileName = uploadedFile.getFileName();
String mimeType = getServletContext().getMimeType(fileName);
if (mimeType.startsWith("text/")) {
    // It's a text.
}else if(mimeType.startsWith("image/")){
    // It's an image.
}

Hope it helps.

SachinSarawgi
  • 2,632
  • 20
  • 28
1

In order to specify the file type, aka Mime Type, you should use either of the 2 args or 3 args constructors of FielBody instead of the one-arg one you are already using.

It should be something as follows, depending on your file type:

FileBody bin = new FileBody(new File(file1), ContentType.DEFAULT_BINARY);
tmarwen
  • 15,750
  • 5
  • 43
  • 62
  • the problem still the same after i add in your code, any idea ? – attack Dec 13 '16 at 09:12
  • @attack The idea is simple. You are responsible for providing "a file extension", not the system. No way around that. – Margaret Bloom Dec 13 '16 at 09:18
  • @MargaretBloom - During the time i read the file, i also need to included the file extension as well ? is it something like that? – attack Dec 13 '16 at 09:26
  • @attack Document yourself about *mime-types* and how HTTP file upload works (Particularly the *Content-Disposition* header). The file name is included by the HttpClient framework (as you can see from the sources) but the mime type is not inferred: is left to the caller to give one or a default binary will be used (and "data" will be detected on server side). I have already linked two questions on how to retrieve the mime-type. If you want to do a quick test, just use "image/png" and see what the server reports. – Margaret Bloom Dec 13 '16 at 09:31
  • @MargaretBloom - if i want to do a quick check, where i shoulod put "image/png"? – attack Dec 13 '16 at 09:50
  • @attack Edited my answer. Don't pollute tmarwen inbox with unsolicited notifications, either comment my answer or just take the effort to use Google. – Margaret Bloom Dec 13 '16 at 09:57
0

You can find the source code for the, at the time of writing, three constructors of FileBody on the relevant repo.

public FileBody(final File file) {
    this(file, ContentType.DEFAULT_BINARY, file != null ? file.getName() : null);
}

/**
 * @since 4.3
 */
public FileBody(final File file, final ContentType contentType, final String filename) {
    super(contentType);
    Args.notNull(file, "File");
    this.file = file;
    this.filename = filename == null ? file.getName() : filename;
}

/**
 * @since 4.3
 */
public FileBody(final File file, final ContentType contentType) {
    this(file, contentType, file != null ? file.getName() : null);
}

If you don't specify a contentType the default is ContentType.DEFAULT_BINARY.
It is the content type that defines the "file type" as not every OS use the file extension for that purpose.


If you are running in a Linux system see How can I find out a files “mime-type(Content-Type?)”? to retrieve the mime type of a file.
There is also a Windows equivalent question.

Once you had the content type in contentType, use the right constructor: new FileBody(new File(file1), ContentType.create(contentType));.
You may want to add encoding information too, see ContentType.


You didn't specify what server you are running, but before going into the labour of detecting the mime-type of a file, you can do a quick test consisting in putting an hardcoded mime-type in the request.
Just construct the FileBody object as

FileBody bin = new FileBody(new File(file1), ContentType.create("image/png"));
Margaret Bloom
  • 41,768
  • 5
  • 78
  • 124