java中的文件传输问题

java中的文件传输问题,java,file-transfer,Java,File Transfer,我有办法 protected String browsesFile() { String url = null; FileDialog dialog = new FileDialog(PlatformUI.getWorkbench() .getActiveWorkbenchWindow().getShell(), SWT.NULL); // set the filter

我有办法

protected String browsesFile() {        
            String url = null;
            FileDialog dialog = new FileDialog(PlatformUI.getWorkbench()
                    .getActiveWorkbenchWindow().getShell(), SWT.NULL);
            // set the filter options
            dialog.setFilterExtensions(new String[] { "*.jpg", "*.jpeg", "*.png" });
            String path = dialog.open();
            if (path != null) {
                File file = new File(path);
                if (file.isFile())
                    url = file.toString();
                else
                    url = file.list().toString();
            }
            return url;
        }// end of method browseFile()
它将带来文件的
url
. 我称之为
text.setText(browsesFile())。这将带来我选择的图像的url。我希望将该图像传输到
G:\myImage
。为了转学,我做了以下工作

    public static void copyFile(File sourceFile, File destFile) throws IOException {
     if(!destFile.exists()) {
      destFile.createNewFile();
     }

     FileChannel source = null;
     FileChannel destination = null;
     try {
      source = new FileInputStream(sourceFile).getChannel();
      destination = new FileOutputStream(destFile).getChannel();
      destination.transferFrom(source, 0, source.size());
     }
     finally {
      if(source != null) {
       source.close();
      }
      if(destination != null) {
       destination.close();
      }
}}
我使用函数作为

File source = new File(text.getText());         
    String url ="G:\\myImage";
    File dest = new File(url);
try {
    copyFile(source, dest);
    } catch (IOException e1) {
    e1.printStackTrace();
    }
    }
我得到的错误消息如下所示

java.io.FileNotFoundException: G:\myImage (Access is denied)
    at java.io.FileOutputStream.open(Native Method)
    at java.io.FileOutputStream.<init>(Unknown Source)
    at java.io.FileOutputStream.<init>(Unknown Source)
java.io.FileNotFoundException:G:\myImage(访问被拒绝)
在java.io.FileOutputStream.open(本机方法)
位于java.io.FileOutputStream。(未知源)
位于java.io.FileOutputStream。(未知源)

可能的原因是什么?我正在使用windows 7

您正在使用一个目录名作为目标,这是您的错误源

您可以通过以下方式将源文件名添加到目标来轻松修复此问题:

File source = new File(text.getText());         
String url ="G:\\myImage";
File dest = new File(url, source.getName() );

仅供参考:
transferFrom
最多可传输到
source.size()
字节。根据,它返回“实际传输的字节数,可能为零”。您可能希望准确地验证实际传输了多少字节,以避免出现细微的错误。错误消息似乎非常明确。检查G:驱动器是否正确映射到计算机上,您的Java程序运行时有足够的权限写入它。@perception G:\是我的本地驱动器。我已经在那里创建了myImage文件夹。我认为它是正确的映射。您是否试图写入与目录同名的文件?与上面的@Captain Giraffe问题相同。是的,感谢absolutley的工作,我非常感谢你投下的一票。我想问你另一个与此相关的问题。我通常需要使用url。每次放斜杠都很单调。我认为在另一种语言中,放@infront不需要放转义序列。java有什么办法吗?