我正在尝试从源文件中移动文件以自动保存到桌面。基本上这是一个传入的文件,我希望它对用户可见,这是他/她的桌面,我认为我在正确的轨道上,但这段代码不起作用,以前的修改不起作用也可以有人指出我在正确的方向?
System.out.println("Waiting For File......");
int filesize = 6022386;
byte[] mybytearray = new byte[filesize];
InputStream is;
is = connection_download.getInputStream();
FileOutputStream fos;
// obtain header (fileName)
DataInputStream clientData = new DataInputStream(is);
String fileName = clientData.readUTF();
OutputStream output = new FileOutputStream(fileName);
System.out.println(fileName);
fos = new FileOutputStream(fileName);
BufferedOutputStream bos = new BufferedOutputStream(fos);
bytesRead = is.read(mybytearray, 0, mybytearray.length);
current = bytesRead;
do {
bytesRead = is.read(mybytearray, current, (mybytearray.length - current));
if (bytesRead >= 0) {
current += bytesRead;
}
} while (bytesRead > -1);
bos.write(mybytearray, 0, current);
bos.flush();
System.out.println(fileName.length());
long end = System.currentTimeMillis();
System.out.println("DONE");
bos.close();
// complete file
complete = new File(fileName);
if (isMacOs == true) {
downloadMac(complete);
} else if (isWindowsOS == true) {
downloadWindows(complete);
}
} catch (IOException ex) {
Logger.getLogger(downloadFile.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
public void downloadMac(File complete) {
JOptionPane.showMessageDialog(null, "Your Files Will be Saved To Your Desktop");
String OS_Path = System.getProperty("user.home");
String savedLocation = OS_Path + "/Desktop/" + complete;
File downloadedLocation = new File(savedLocation);
System.out.println(downloadedLocation);
}