4

I am trying to send a file to the controller by using it in FormBodyPart, rather than directly sending the file to it. Here is the code for making a collection of files

private void addFile(Collection<FormBodyPart> parts, File inputFile, String fileType)
        throws ClassificationException {
    if (inputFile == null) {
        throw new ClassificationException("Null input file provided");
    }
    if (!inputFile.exists()) {
        throw new ClassificationException("Input file not found: " + inputFile.getAbsolutePath());
    }
    if (fileType != null) {

        String charset = "UTF-8";
        parts.add(new FormBodyPart("file", new FileBody(inputFile, fileType, charset)));

    } else {
        parts.add(new FormBodyPart("file", new FileBody(inputFile, inputFile.getName())));
    }
}

Parts collection is an arraylist, which would contain the files.

Here is my code for setting Http Entity

HttpPost httppost = new HttpPost("http://localhost:9000/upload1");
            MultipartEntity reqEntity1 = new MultipartEntity();
            FormBodyPart part1;
            for (Iterator i$ = parts.iterator(); i$.hasNext(); reqEntity1.addPart(part1)) {
                part1 = (FormBodyPart) i$.next();
                System.out.println(part1.getHeader());
            }

            httppost.setEntity(reqEntity1);
            HttpResponse response = httpclient.execute(httppost);
            System.out.println(response);

My method declaration of controller is

String index(@RequestParam("file") MultipartFile uploadfile)

I am getting an error from the server stating

[400] {"timestamp":1474898550131,"status":400,"error":"Bad Request","exception":"org.springframework.web.multipart.support.MissingServletRequestPartException","message":"Required request part 'file' is not present","path":"/upload1"}

My dispatcher.xml already contains bean of multipartResolver.

I am fairly new to web services and might be doing some kind of silly mistake. Please help me out,thanks in advance

4
  • You should not use $ in variable names. Java Language Specification §3.8: The $ sign should be used only in mechanically generated source code or, rarely, to access pre-existing names on legacy systems.
    – Andreas
    Commented Sep 26, 2016 at 14:47
  • Are you sure parts is not empty?
    – Andreas
    Commented Sep 26, 2016 at 14:50
  • When i am printing parts its showing "[org.apache.http.entity.mime.FormBodyPart@1fb3ebeb]", so i dont think it is empty. Commented Sep 26, 2016 at 15:00
  • i did change the name of my variable from i$ to i. its showing the same result. Commented Sep 26, 2016 at 15:03

2 Answers 2

3

Verify if you have this items:

@Bean
public CommonsMultipartResolver multipartResolver() {
    CommonsMultipartResolver multipart = new CommonsMultipartResolver();
    multipart.setMaxUploadSize(3 * 1024 * 1024);
    return multipart;
}

@Bean
@Order(0)
public MultipartFilter multipartFilter() {
    MultipartFilter multipartFilter = new MultipartFilter();
    multipartFilter.setMultipartResolverBeanName("multipartResolver");
    return multipartFilter;
}

and in the applications.properties:

# MULTIPART (MultipartProperties)
spring.http.multipart.enabled=true 
# Enable support of multi-part uploads.
# spring.http.multipart.file-size-threshold=3 # Threshold after which files will be written to disk. Values can use the suffixed "MB" or "KB" to indicate a Megabyte or Kilobyte size.
spring.http.multipart.location= /
# Intermediate location of uploaded files.
spring.http.multipart.max-file-size=10MB
# Max file size. Values can use the suffixed "MB" or "KB" to indicate a Megabyte or Kilobyte size.
spring.http.multipart.max-request-size=10MB
# Max request size. Values can use the suffixed "MB" or "KB" to indicate a Megabyte or Kilobyte size.
spring.http.multipart.resolve-lazily=false 
# Whether to resolve the multipart request lazily at the time of file or parameter access.
0
-1

there is a very good example in spring.io Uploading Files.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.