0

我想从闪存上传文件,我的原始请求(用 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--

我想保存发布的文件

@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");
        }
    }

但没有运气,我在 List items = upload.parseRequest(request); 中看不到任何项目。由于要求,我无法更改请求标头。

4

1 回答 1

1

解决了!问题出在 spring 配置 xml 文件中。从以前的实现离开

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

所以春天在我的方法之前阅读 HttpRequest 和

upload.parseRequest(request)

读取空流。

于 2013-05-15T05:39:39.317 回答