YUI Uploader与Java后端

时间:2022-08-28 12:19:41

I am trying to use the (flash based) YUI Uploader with a Java (Spring-based) back-end.

我正在尝试使用(基于闪存的)YUI上传器和Java(基于Spring)的后端。

The typical way of uploading files in the Java Servlet world is to set the ENCTYPE='multipart/form-data' on the HTML form requesting the file from the user. With the right server side APIs (i.e. Commons FileUpload), it is possible to get the file on the server.

在Java Servlet世界中上传文件的典型方法是在HTML表单上设置ENCTYPE ='multipart / form-data',从而向用户请求文件。使用正确的服务器端API(即Commons FileUpload),可以在服务器上获取该文件。

But I am stymied by how to achieve this with the YUI Uploader. I am able to reach the Java controller, and I am even able to extract the custom post values. But I have no idea how to extract the binary file data out of the request.

但我对如何使用YUI Uploader实现这一点感到困惑。我能够访问Java控制器,甚至可以提取自定义帖子值。但我不知道如何从请求中提取二进制文件数据。

Has anyone out had any luck with a YUI uploader with a Java back-end?

有没有人对带有Java后端的YUI上传器有任何好运?

1 个解决方案

#1


To answer my own question, and to make a long story short, this snippet of code did the trick:

要回答我自己的问题,并且长话短说,这段代码就可以解决问题:

@Controller
@RequestMapping("/FileUploadController")
public class FileUploadController {

    @RequestMapping(method = RequestMethod.POST)
    protected ModelAndView onSubmit(HttpServletRequest request) throws Exception{
        FileItemFactory factory = new DiskFileItemFactory();
        ServletFileUpload upload = new ServletFileUpload(factory);
        List<FileItem> /* FileItem */ items = upload.parseRequest(request);

        for (FileItem fileItem : items) {
            if (fileItem.isFormField()) {
//                processFormField(fileItem);
            } else {
                File uploadedFile = new File("/tmp/junk/" + fileItem.getName());
                fileItem.write(uploadedFile);
            }
        }
        return new ModelAndView("index");
    }
}

This example uses Spring, but you should be able to do exactly the same as long as you have HttpServletRequest object.

此示例使用Spring,但只要您拥有HttpServletRequest对象,您就应该能够完全相同。

#1


To answer my own question, and to make a long story short, this snippet of code did the trick:

要回答我自己的问题,并且长话短说,这段代码就可以解决问题:

@Controller
@RequestMapping("/FileUploadController")
public class FileUploadController {

    @RequestMapping(method = RequestMethod.POST)
    protected ModelAndView onSubmit(HttpServletRequest request) throws Exception{
        FileItemFactory factory = new DiskFileItemFactory();
        ServletFileUpload upload = new ServletFileUpload(factory);
        List<FileItem> /* FileItem */ items = upload.parseRequest(request);

        for (FileItem fileItem : items) {
            if (fileItem.isFormField()) {
//                processFormField(fileItem);
            } else {
                File uploadedFile = new File("/tmp/junk/" + fileItem.getName());
                fileItem.write(uploadedFile);
            }
        }
        return new ModelAndView("index");
    }
}

This example uses Spring, but you should be able to do exactly the same as long as you have HttpServletRequest object.

此示例使用Spring,但只要您拥有HttpServletRequest对象,您就应该能够完全相同。