0

I want upload file from flash, my raw request (cached with Fiddler):

    POST /api/v1/exercises/uploadTableImage HTTP/1.1
    x-flash-version: 11,2,202,228
    Content-Type: multipart/form-data; boundary=jdmlcuucuseqxyyyvsfjbfixukdbuesq
    Cache-Control: no-cache
    Content-Length: 93737
    User-Agent: Shockwave Flash
    Host: localhost:8080
    Pragma: no-cache
    Cookie: JSESSIONID=BA009CDDBD828B931FCC3B0894FD7DCD;

    --jdmlcuucuseqxyyyvsfjbfixukdbuesq
    Content-Disposition: form-data; name="Filename"

    20er_1_1.jpg.jpg
    --jdmlcuucuseqxyyyvsfjbfixukdbuesq
    Content-Disposition: form-data; name="filedata"; filename="20er_1_1.jpg.jpg"
    Content-Type: application/octet-stream

    1


    --jdmlcuucuseqxyyyvsfjbfixukdbuesq
    Content-Disposition: form-data; name="Upload"

    Submit Query
    --jdmlcuucuseqxyyyvsfjbfixukdbuesq--

I want save posted file

@Controller
@RequestMapping(API_ROOT+"exercises")
public class ImageUploadingController {

    private final String imagesWebPath = "uploaded";

    @RequestMapping(value = "uploadTableImage", method = POST)
    public void uploadImage(HttpServletRequest request) throws Exception {

        FileItemFactory factory = new DiskFileItemFactory();

        ServletFileUpload upload = new ServletFileUpload(factory);

        // Parse the request
        FileItem uploadedFile = null;
        List<FileItem> items = upload.parseRequest(request);
        for (FileItem item : items) {
            if ("Filedata".equals(item.getFieldName())) {
                uploadedFile = item;
                break;
            }
        }

        if (uploadedFile != null) {
            File file = new File(request.getRealPath(imagesWebPath)+File.separator+uploadedFile.getName());
            file.getParentFile().mkdirs();
            file.createNewFile();
            uploadedFile.write(file);

        } else {
            throw new RuntimeException("No files found");
        }
    }

But no luck, I see no items in List items = upload.parseRequest(request);. Due to requirements I cannot change request headers.

2
  • The line List<FileItem> items = upload.parseRequest(request); does not compile because of Error:(56, 52) java: incompatible types: javax.servlet.http.HttpServletRequest cannot be converted to org.apache.tomcat.util.http.fileupload.RequestContext. Maybe it is down to a different version of Tomcat ? Commented Aug 3, 2016 at 14:29
  • It turns out that Tomcat contains this package org.apache.tomcat.util.http.fileupload that contains the same yet slightly incopatible classes contained in the original org.apache.commons.fileupload package. My bad! Commented Aug 3, 2016 at 14:52

1 Answer 1

1

Solved! Problem was in spring configuration xml file. From previous implementation left

<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        <property name="maxUploadSize" value="100000"/>
</bean>

So spring read HttpRequest before my method and

upload.parseRequest(request)

read empty stream.

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

Comments

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.