vendredi 10 avril 2015

Java - Paste a file from clipboard after it has been downloaded

I have some trouble with the Windows Explorer.


My Application starts the download of a file and puts it into the system clipboard meanwhile. If the User now pastes the file at some place of the Windows Explorer, the file isn't having its real size, but the size depending on the progress of the download.


Does somebody have an idea of how I could advise the Explorer to wait until the download has finished and copy the file to the target directory afterwards?


I also tried to "tunnel" the file over the loopback Interface using the local SMB server but it didnt help unfortunately...


Thanks in advance


Downloading the file and calling the paste to clipboard method:



new Thread(new Runnable() {

@Override
public void run() {

final String where = getText();

int selectedRows[] = CustomJTableModel.table.getSelectedRows();

List<File> fileList = new ArrayList<File>();

for( int i=0; selectedRows.length>i; i++ ) {

// Build the relative file/folder path

String fileName = (String)table.getValueAt(table.getSelectedRow(), 0);

final String value = buildPath(where, fileName);

new Thread(new Runnable() {

@Override
public void run() {

staticNetworkDaemon.getFile(value, where);

}

}).start();

fileList.add(new File("\\\\127.0.0.1\\r$\\downloaded" + value.replace("/", "\\")));

}

setClipboardFile(fileList);

}

}).start();


Copying the file to the clipboard:



public static void setClipboardFile(final List<File> files) {

Transferable trans = new Transferable() {

List<File> fileList = new ArrayList<File>();

public DataFlavor[] getTransferDataFlavors() {

for( File f: files ) {

fileList.add(f);

}

return new DataFlavor[] { DataFlavor.javaFileListFlavor };

}

public boolean isDataFlavorSupported(DataFlavor flavor) {

return DataFlavor.javaFileListFlavor.equals(flavor);

}

public Object getTransferData(DataFlavor flavor) throws UnsupportedFlavorException, IOException {

if (isDataFlavorSupported(flavor))

return fileList;

throw new UnsupportedFlavorException(flavor);

}

};

Toolkit.getDefaultToolkit().getSystemClipboard().setContents(trans, null);

}

Aucun commentaire:

Enregistrer un commentaire