使用Blazeds和Flex将文件上传到文件夹?

时间:2013-02-09 10:47:38

标签: java spring hibernate flex blazeds

在我的Flex应用程序中,我正在使用Blazeds进行图像上传...

    private var fileReference:FileReference;
        protected function imageUpload(event:MouseEvent):void
        {
            // create a fileFilter - class declaration
            var imageTypes:FileFilter;
            // set the file filter type - jpg/png/gif - init method             
            imageTypes = new FileFilter("Images (*.jpg, *.jpeg, *.gif, *.png)", "*.jpg; *.jpeg; *.gif; *.png");

            fileReference = new FileReference();

            fileReference.browse([imageTypes]);

            fileReference.addEventListener(Event.SELECT, browseImage);              
            fileReference.addEventListener(Event.COMPLETE, uploadImage);                
        }

        private function browseImage(event:Event):void {
            fileReference.load();
        }
        private function uploadImage(event:Event):void {
            profileImage.source = fileReference.data;

            var name:String = fileReference.name;
            var directory:String = "/EClassV1/flex_src/Images";
            var content:ByteArray = new ByteArray();
            fileReference.data.readBytes(content, 0, fileReference.data.length);
            var fileAsyn:AsyncToken = userService.uploadImage(name,directory,content);
            fileAsyn.addResponder(new mx.rpc.Responder(handler_success, handler_failure)); 
        }

在我的Java代码中......

@RemotingInclude
public void uploadImage(String name, String directory, byte[] content) {
    File file = new File(directory);
    if (!file.exists()) {
        file.mkdir();
    }
    name = directory + "/" + name;
    File fileToUpload = new File(name);
    try {
        FileOutputStream fos = new FileOutputStream(fileToUpload);
        fos.write(content);
        System.out.println("file write successfully");
        fos.close();
    } catch (Exception ex) {
        ex.printStackTrace();
   }

}

但它给......错误..

java.io.FileNotFoundException: \EClassV1\flex_src\Images\image001.png (The system cannot find the path specified)
at java.io.FileOutputStream.open(Native Method)
at java.io.FileOutputStream.<init>(FileOutputStream.java:194)

其实我想把文件传入文件夹并存储数据库.. 救救我..

1 个答案:

答案 0 :(得分:0)

如果文件尚不存在,则需要创建该文件。方法createNewFile()将为您执行此操作:

File fileToUpload = new File(name);
fileToUpload.createNewFile();

try {
    FileOutputStream oFile = new FileOutputStream(fileToUpload, false); 
    ...