使用 Flex UI 和 Spring/Hibernate/BlazeDS 后端进行简单的文件上传?

发布于 2024-09-10 04:53:34 字数 4889 浏览 4 评论 0原文

我是 Flex 和 BlazeDS 的新手,我正在尝试实现一个简单的应用程序,该应用程序在前端使用 Flex,在后端使用 Spring/Hibernate 应用程序,两者之间通过 BlazeDS 通道进行通信。

我正在寻求有关解决此问题的最佳和/或最简单方法的方向。我的 UI 设置方式是向用户显示一个文件选择器,用户可以在其中选择要上传的图像文件。当选择并提交(作为表单提交)时,服务器端应该接收图像文件数据以及一些相关元数据,例如描述和日期,然后使用图像文件数据和相关元数据填充 Hibernate 实体/POJO ,然后将实体/POJO 持久化到数据库中。

我在此处找到了一些如何使用 servlet 进行文件上传和下载的示例 和 FileReference 类(此处 和此处),但这些不会出现以利用 BlazeDS 和/或 Spring/Hibernate 的方式解决问题。我想将图像文件数据和相关元数据(描述、捕获日期等)放入 Flex 应用程序内的值对象中,然后通过 BlazeDS 将其发送到 Tomcat 上运行的 Spring/Hibernate 应用程序提供的服务。在此服务中,我想从 Flex 应用程序发送的值对象中提取图像数据(实际的 JPG/PNG/GIF 数据和相关元数据,例如描述等)到实体/POJO 中,然后通过在我的数据库中休眠。

这可以做到吗?如果可以的话,最好的方法是什么?我是否错误地假设,如果我使用 BlazeDS,那么我会以某种方式绕过在服务器端提供基于 HTTP 的服务(例如 servlet)的需要,而可以将我的 Java 服务用作“RemoteObjects”?在进行这种传输时,Java POJO/实体类和 Flex 值对象类之间是否必须存在一对一的映射?如果是的话,是否有一个工具可以从 Java POJO 创建相应的 Flex 值对象,反之亦然。

提前感谢您的帮助、评论、建议等。

--James

更新:一些代码可以使这一点更清楚:

我在 Flex 中将此作为我的值对象:

package valueobjects
{
    import flash.utils.ByteArray;

    [Bindable]
    [RemoteClass(alias="com.abc.example.persistence.entity.Image")]

    public class Image
    {
        public var id:Number;
        public var captureDate:Date;
        public var description:String;
        public var imageData:ByteArray;

        public function Image() {}        
    }

我假设这可以是用作到服务端使用的 POJO 类和服务器端 DAO 类的一对一映射,如下所示:

package com.abc.example.persistence.entity;

import java.sql.Blob;
import java.util.Date;
import javax.persistence.Column;
import javax.persistence.Entity;

@Entity(name = "IMAGE")
public class Image
    extends AbstractBaseEntity<Long>
{
    private String description;
    private Date captureDate;
    private Blob imageData;

    @Column(name = "CAPTURE_DATE", nullable = true)
    public Date getCaptureDate()
    {
        return captureDate;
    }

    @Column(name = "DESCRIPTION", nullable = true)
    public String getDescription()
    {
        return description;
    }

    @Column(name = "IMAGE_DATA", nullable = true)
    public Blob getImageData()
    {
        return imageData;
    }

    public void setCaptureDate(final Date captureDate)
    {
        this.captureDate = captureDate;
    }

    public void setDescription(final String description)
    {
        this.description = description;
    }

    public void setImageData(final Blob imageData)
    {
        this.imageData = imageData;
    }
}

在我的 Flex 应用程序中,我使用描述字符串、日期、和图像文件数据(基于用户的文件选择和描述文本输入),然后调用映射到 Tomcat 上运行的服务的 RemoteObject 上的方法。我使用 Image 值对象作为参数在 Flex 代码中进行 RemoteObject 服务调用,但在服务器端运行的服务方法实际上需要 POJO/实体类型的参数,在这里我认为某种Flex 值对象和 Java POJO 之间的转换/转换将会发生(凭借值对象的类声明上的 RemoteClass 别名设置),但它似乎不会以这种方式发生,因为当我调试应用程序时,Java 服务仅在进行服务调用时获取空值。

在我的 Flex 应用程序中,我有一个 FileReference 和 Image 值对象作为公共可绑定变量:

[Bindable]
public var imageToBeArchivedFileReference:FileReference = new FileReference();
[Bindable]
public var imageToBeArchivedValueObject:valueobjects.Image = new valueobjects.Image();

还有一个事件处理程序用于在用户单击文件选择按钮时浏览文件:

protected function imageFileSelectButton_clickHandler(event:MouseEvent):void
{
    var imageFileFilter:FileFilter = new FileFilter("Images (*.jpg, *.jpeg, *.gif, *.png)", "*.jpg;*.jpeg;*.gif;*.png");
    var fileTypes:Array = new Array();
    fileTypes.push(imageFileFilter);
    imageToBeArchivedFileReference.addEventListener(Event.SELECT, imageToBeArchived_fileSelectHandler);
    imageToBeArchivedFileReference.browse(fileTypes);
}

有一个事件处理程序在以下情况下构建值对象:图像文件已被选择:

private function imageToBeArchived_fileSelectHandler(event:Event):void
{
    imageToBeArchivedFileReference.load();
    imageToBeArchivedValueObject = new valueobjects.Image()
    imageToBeArchivedValueObject.imageData = imageToBeArchivedFileReference.data;
    imageToBeArchivedValueObject.description = imageToBeArchivedDescription.text;
    imageToBeArchivedValueObject.captureDate = imageToBeArchivedFileReference.creationDate;
}

并且当用户单击提交按钮执行图像保存/上传时,会调用一个事件处理程序:

protected function archiveImageButton_clickHandler(event:MouseEvent):void
{
    imageArchivalService.archiveImage(imageToBeArchived);
}

在服务器端,我的 Java 类正在执行 POJO 的简单保存:

public void archiveImage(final Image image)
{
    imageDao.saveOrUpdate(image);
}

当我设置在上面的方法中设置一个断点,然后查看图像变量,它看起来是空的,所以我假设从 Flex 值对象到 Java POJO 的转换没有按预期进行,并且不仅仅是添加一个Flex 值对象类中的 RemoteClass 别名。

I'm new to Flex and BlazeDS and I'm trying to implement a simple application which uses Flex on the front end and a Spring/Hibernate application on the back end, with communication between the two going over a BlazeDS channel.

I'm seeking direction as to the best and/or simplest way to approach this. I have the UI set up in such a way that the user is presented with a file chooser in which they pick the image file they want to upload. When this is chosen and submitted (as a form submission) then the server side should receive the image file data as well as some related metadata such as a description and date, then populate a Hibernate entity/POJO with the image file data and related metadata, and then persist the entity/POJO into the database.

I have found some examples of how you would do a file upload and download using servlets here and the FileReference class (here and here) but these don't appear to address the problem in a way which leverages BlazeDS and/or Spring/Hibernate. I want to put the image file data and related metadata (description, capture date, etc.) into a value object within the Flex application and then send this over BlazeDS to a service provided by my Spring/Hibernate application running on Tomcat. In this service I want to extract the image data (both the actual JPG/PNG/GIF data and the related metadata such as description, etc.) from the value object sent from the Flex app into an entity/POJO which is then persisted via Hibernate in my database.

Can this be done, and if so what's the best way to go about it? Am I mistaken in assuming that if I use BlazeDS then I am somehow bypassing the need to provide HTTP-based services such as servlets on the server side and instead I can use my Java services as "RemoteObjects"? Is there necessarily a one-to-one mapping between Java POJO/entity class and the Flex value object class when making this sort of transfer? If so is there a tool which creates corresponding Flex value objects from Java POJOs or vice versa.

Thanks in advance for your help, comments, suggestions, etc.

--James

Update: Some code to make this more clear:

I have this as my value object in Flex:

package valueobjects
{
    import flash.utils.ByteArray;

    [Bindable]
    [RemoteClass(alias="com.abc.example.persistence.entity.Image")]

    public class Image
    {
        public var id:Number;
        public var captureDate:Date;
        public var description:String;
        public var imageData:ByteArray;

        public function Image() {}        
    }

I am assuming that this can be used as a one-to-one mapping to the POJO class used by my service and DAO classes on the server-side, which looks like this:

package com.abc.example.persistence.entity;

import java.sql.Blob;
import java.util.Date;
import javax.persistence.Column;
import javax.persistence.Entity;

@Entity(name = "IMAGE")
public class Image
    extends AbstractBaseEntity<Long>
{
    private String description;
    private Date captureDate;
    private Blob imageData;

    @Column(name = "CAPTURE_DATE", nullable = true)
    public Date getCaptureDate()
    {
        return captureDate;
    }

    @Column(name = "DESCRIPTION", nullable = true)
    public String getDescription()
    {
        return description;
    }

    @Column(name = "IMAGE_DATA", nullable = true)
    public Blob getImageData()
    {
        return imageData;
    }

    public void setCaptureDate(final Date captureDate)
    {
        this.captureDate = captureDate;
    }

    public void setDescription(final String description)
    {
        this.description = description;
    }

    public void setImageData(final Blob imageData)
    {
        this.imageData = imageData;
    }
}

In my Flex application I populate the fields of an Image object with a description string, date, and image file data (based on the user's file selection and text input for the description) and then call a method on the RemoteObject which is mapped to the service running on Tomcat. I make the RemoteObject service call within my Flex code using the Image value object as the argument, but the service method running on the servier side actually expects an argument of the POJO/entity type, and it's here that I am thinking that some sort of conversion/transformation between the Flex value object and the Java POJO will occur (by virtue of the RemoteClass alias setting on the value object's class declaration), but it doesn't seem to be happening that way because when I debug the application the Java service is only getting null values when the service call is made.

In my Flex application I have a FileReference and Image value object as public, bindable variables:

[Bindable]
public var imageToBeArchivedFileReference:FileReference = new FileReference();
[Bindable]
public var imageToBeArchivedValueObject:valueobjects.Image = new valueobjects.Image();

There is also an event handler to browse for a file when the user clicks on a file select button:

protected function imageFileSelectButton_clickHandler(event:MouseEvent):void
{
    var imageFileFilter:FileFilter = new FileFilter("Images (*.jpg, *.jpeg, *.gif, *.png)", "*.jpg;*.jpeg;*.gif;*.png");
    var fileTypes:Array = new Array();
    fileTypes.push(imageFileFilter);
    imageToBeArchivedFileReference.addEventListener(Event.SELECT, imageToBeArchived_fileSelectHandler);
    imageToBeArchivedFileReference.browse(fileTypes);
}

There is an event handler which builds the value object when the image file has been selected:

private function imageToBeArchived_fileSelectHandler(event:Event):void
{
    imageToBeArchivedFileReference.load();
    imageToBeArchivedValueObject = new valueobjects.Image()
    imageToBeArchivedValueObject.imageData = imageToBeArchivedFileReference.data;
    imageToBeArchivedValueObject.description = imageToBeArchivedDescription.text;
    imageToBeArchivedValueObject.captureDate = imageToBeArchivedFileReference.creationDate;
}

and there's an event handler which is invoked when the user clicks on the submit button to perform the image save/upload:

protected function archiveImageButton_clickHandler(event:MouseEvent):void
{
    imageArchivalService.archiveImage(imageToBeArchived);
}

On the server side my Java class is doing a simple save of the POJO:

public void archiveImage(final Image image)
{
    imageDao.saveOrUpdate(image);
}

When I set a breakpoint in the method above and look at the image variable it looks to be empty, so I'm assuming that the transformation from the Flex value object to the Java POJO did not go as expected and that there's more to it than just adding a RemoteClass alias in the Flex value object class.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

指尖上的星空 2024-09-17 04:53:34

看看这个例子,一切都在那里。

http://biemond.blogspot.com/ 2008/08/flex-upload-and-download-with-blazeds.html

不要使用加载器类,使用 readBytes 调用。

请务必查看评论,那里有有价值的信息。

干杯

Check out this example, it is all there.

http://biemond.blogspot.com/2008/08/flex-upload-and-download-with-blazeds.html

Don't use the loader class, use the readBytes call.

Make sure you go to the comments, there are valuable info there.

Cheers

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文