简体   繁体   English

使用Java中的multipart / form-data获取来自HTTP POST请求的数据

[英]Get data form HTTP POST request with multipart/form-data in Java (Spring)

I want upload file from flash, my raw request (cached with Fiddler): 我想从Flash上​​传文件,这是我的原始请求(已用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);. 但没有运气,我没有看到List items = upload.parseRequest(request);中的任何项目。 Due to requirements I cannot change request headers. 由于要求,我无法更改请求标头。

Solved! 解决了! Problem was in spring configuration xml file. 问题出在spring配置xml文件中。 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 所以春天在我的方法之前读了HttpRequest和

upload.parseRequest(request)

read empty stream. 读取空流。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 多部分/表单数据发布-Java Spring - Multipart/Form-Data Post - Java Spring Spring POST multipart/form-data,请求部分始终为空 - Spring POST multipart/form-data, request parts always empty 使用 java 创建一个 HTTP 多部分/表单数据请求 - creating an HTTP multipart/form-data request with java Java-使用远程文件(带有http://或file://协议)的multipart / form-data POST请求 - Java - multipart/form-data POST request using a remote file (with http:// or file:// protocols) 从 Camel 中的 multipart/form-data HTTP POST 请求中获取键值对 - Get key-value pairs from multipart/form-data HTTP POST request in Camel HTTP POST 请求使用 java 中的表单数据返回 400 - HTTP POST request using form-data in java returns 400 与多部分/表单数据上传表单一起传递参数(Java Http 上传后) - Passing parameters along with a multipart/form-data upload form (Java Http Post Upload) 如何使用Java Spring将带有上传文件的多部分/表单数据表单重新发送到其他服务器 - How to resend post multipart/form-data form with upload file to different server with Java Spring Spring无法处理“多部分/表单数据” POST请求(错误400“错误请求”) - Spring cannot process “multipart/form-data” POST request (error 400 “Bad request”) 解析 `multipart/form-data` http 请求是如何工作的? - How does parsing the `multipart/form-data` http request work?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM