0

在我的 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)

实际上我想将文件放入文件夹并存储数据库..帮帮我..

4

1 回答 1

0

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

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

try {
    FileOutputStream oFile = new FileOutputStream(fileToUpload, false); 
    ...
于 2013-02-09T10:52:57.793 回答