3

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?

1
  • not error code return from server, the file reached my server Commented Dec 13, 2016 at 8:44

3 Answers 3

2

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.

Sign up to request clarification or add additional context in comments.

Comments

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);

6 Comments

the problem still the same after i add in your code, any idea ?
@attack The idea is simple. You are responsible for providing "a file extension", not the system. No way around that.
@MargaretBloom - During the time i read the file, i also need to included the file extension as well ? is it something like that?
@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.
@MargaretBloom - if i want to do a quick check, where i shoulod put "image/png"?
|
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"));

2 Comments

it's able to detect ASCII file but it's fail for others file type, any idea?
@attack Without a minimal reproducible example and the server side code is hard to tell.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.