通过Java发送文件
我的代码是:通过Java发送文件,java,file,copy,Java,File,Copy,我的代码是: public class RemotePlay { static final String USER_NAME = "bwisniewski"; static final String PASSWORD = "xxx"; static final String NETWORK_FOLDER = "smb://192.168.1.141/ADMIN$/"; public static void main(String[] args) throws IOException, Int
public class RemotePlay {
static final String USER_NAME = "bwisniewski";
static final String PASSWORD = "xxx";
static final String NETWORK_FOLDER = "smb://192.168.1.141/ADMIN$/";
public static void main(String[] args) throws IOException, InterruptedException {
// TODO Auto-generated method stub
String fileContent = "This is a test File";
new RemotePlay().copyFiles(fileContent, "testFile1.txt");
}
public boolean copyFiles(String fileContent, String fileName) {
boolean successful = false;
try{
String user = USER_NAME + ":" + PASSWORD;
System.out.println("User: "+user);
NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication(user);
String path = NETWORK_FOLDER + fileName;
System.out.println("Path: "+path);
SmbFile sFile = new SmbFile(path, auth);
SmbFileOutputStream sfos = new SmbFileOutputStream(sFile);
sfos.write(fileContent.getBytes());
successful = true;
System.out.println("Successful "+successful);
}
catch(Exception e) {
successful = false;
e.printStackTrace();
}
return successful;
}}
如何将其更改为将exe文件发送到ADMIN$share。我更喜欢使用这种方法,因为我必须对远程pc进行身份验证。如果您有更好的想法将文件复制到ADMIN$share,我期待听到您的建议
谢谢
sfos.write(fileContent.getBytes());
如果您的数据是文本,那么为什么不使用PrintWriter来写下您的文件呢
public static void main(String [] args) throws Exception { // temporary
File fileOne = new File("testfile1.txt");
PrintWriter writer = new PrintWriter(fileOne);
// write down data
writer.println("This is a test File");
// free resources
writer.flush();
writer.close();
}
关于扩展名,您可以在创建文件时使用任何您想要的扩展名,它仍将保存数据,如果您将其重命名为硬盘上正确的扩展名,则可以打开该文件
如果您将文件命名为testfile.exe,它仍将保存您的数据,但当您双击它时,它将无法工作,直到您将其重命名为testfile.txt(或者如果扩展名与文件中的数据兼容,它将工作)确定,但我不会将文件从本地计算机复制到远程计算机。类似setup.exe或music.mp3的文件