In my Flex Application i'm doing image Uploading Using 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)); 
        }

And in my Java Code...

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

}

But it giving... error..

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)

Actually i want to Sore file into folder and store Database.. Help me..

有帮助吗?

解决方案

You need to create the file if it does not exist yet. The method createNewFile() will do this for you:

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

try {
    FileOutputStream oFile = new FileOutputStream(fileToUpload, false); 
    ...
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top