哪位大侠有 “根据图片本地路径地址上传图片到Linux服务器”的JAVA代码呀?
您好,提问者:
1、这个我建议使用FTP传输,如果不是自动传输的可以使用FTP方便。
2、写程序的话要建立Socket客户端、和 ServerSocket服务端才可以实现。
可以用ftp实现文件的上传下载,跟linux还是windows没有关系。
把linux作为remote server就可以了。
public static void backupUploadWithCommonsFTP(File fileToBeUpload) {
FTPClient f = new FTPClient();
boolean backupDirectoryExist = false;
boolean fileToBeUploadExist = false;
FTPFile backupDirectory = null;
try {
fconnect(servergetServer());
flogin(servergetUsername(), servergetPassword());
FTPFile[] directories = flistDirectories();
// Check for existence of backup directory
for (FTPFile file : directories) {
String filename = filegetName();
if (fileisDirectory() && filenameequalsIgnoreCase("backup")) {
backupDirectory = file;
backupDirectoryExist = true;
break;
}
}
if (!backupDirectoryExist) {
fmakeDirectory("backup");
}
// Check if file already exist on the server
fchangeWorkingDirectory("files");
FTPFile[] files = flistFiles();
fchangeWorkingDirectory("backup");
String filePathToBeBackup="/home/user/backup/";
String prefix;
String suffix;
String fileNameToBeBackup;
FTPFile fileReadyForBackup = null;
fsetFileType(FTPBINARY_FILE_TYPE);
fsetFileTransferMode(FTPBINARY_FILE_TYPE);
for (FTPFile file : files) {
if (fileisFile() && filegetName()equals(fileToBeUploadgetName())) {
prefix = FilenameUtilsgetBaseName(filegetName());
suffix = ""concat(FilenameUtilsgetExtension(filegetName()));
fileNameToBeBackup = prefixconcat(CalendargetInstance()getTime()toString()concat(suffix));
filePathToBeBackup = filePathToBeBackupconcat(fileNameToBeBackup);
fileReadyForBackup = file;
fileToBeUploadExist = true;
break;
}
}
// If file already exist on the server create a backup from it otherwise just upload the file
if(fileToBeUploadExist){
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
fretrieveFile(fileReadyForBackupgetName(), outputStream);
InputStream is = new ByteArrayInputStream(outputStreamtoByteArray());
if(fstoreUniqueFile(filePathToBeBackup, is)){
JOptionPaneshowMessageDialog(null, "Backup succeeded");
fchangeWorkingDirectory("files");
boolean reply = fstoreFile(fileToBeUploadgetName(), new FileInputStream(fileToBeUpload));
if(reply){
JOptionPaneshowMessageDialog(null,"Upload succeeded");
}else{
JOptionPaneshowMessageDialog(null,"Upload failed after backup");
}
}else{
JOptionPaneshowMessageDialog(null,"Backup failed");
}
}else{
fchangeWorkingDirectory("files");
fsetFileType(FTPBINARY_FILE_TYPE);
fenterLocalPassiveMode();
InputStream inputStream = new FileInputStream(fileToBeUpload);
ByteArrayInputStream in = new ByteArrayInputStream(FileUtilsreadFileToByteArray(fileToBeUpload));
boolean reply = fstoreFile(fileToBeUploadgetName(), in);
Systemoutprintln("Reply code for storing file to server: " + reply);
if(!fcompletePendingCommand()) {
flogout();
fdisconnect();
Systemerrprintln("File transfer failed");
Systemexit(1);
}
if(reply){
JOptionPaneshowMessageDialog(null,"File uploaded successfully without making backup" +
"\nReason: There wasn't any previous version of this file");
}else{
JOptionPaneshowMessageDialog(null,"Upload failed");
}
}
//Logout and disconnect from server
inclose();
flogout();
fdisconnect();
} catch (IOException e) {
eprintStackTrace();
}
}
你是想从服务器下载文件吧?那应该用retrieveFile(String remote, OutputStream local),而不是storefile啊,自己看看API
http://commonsapacheorg/proper/commons-net/apidocs/org/apache/commons/net/ftp/FTPClienthtml
0条评论