2013-02-09 53 views
0

在我的Flex应用程序我做图片上传使用BlazeDS的...使用Blazeds和Flex将文件上传到文件夹?

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) 

其实我想疮文件夹入和存储数据库.. 帮助我..

回答

0

如果文件不存在,您需要创建该文件。该方法createNewFile()会为你做到这一点:

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

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

错误:java.io.IOException异常:系统找不到指定的路径 \t在java.io.WinNTFileSystem.createFileExclusively(本机方法) \t在java.io .File.createNewFile(File.java:883) – 2013-02-09 11:03:42

+0

但我给根目录/子目录/子目录/ 这是错误的方式或不?其他明智的如何给flex目录中的变量。 – 2013-02-09 11:05:11

+1

@NarasimhamK所以你正在WinNT机器上创建一个UNIX风格路径'/ a/b/c'的文件?你尝试过一个WinNT风格的路径吗? – harpun 2013-02-09 11:05:37

相关问题