我想通过ftp服务器从用户界面上传文件。我知道在java上传文件的过程,但我预先定义了代码中的文件路径,但我想手动上传文件(用户界面)如何使用java和java servelet页面完成?有什么建议?
下面是我的java代码
public static void main(String[] args) throws IOException {
FTPClient client = new FTPClient();
FileInputStream fis = null;
boolean result;
try {
client.connect("localhost");
result = client.login("admin", "password");
if (result == true) {
System.out.println("Successfully logged in!");
} else {
System.out.println("Login Fail!");
return;
}
File file = new File("C:\\Users\\uploadexample.txt");
String testName = file.getName();
fis = new FileInputStream(file);
// Upload file to the ftp server
result = client.storeFile(testName, fis);
if (result == true) {
System.out.println("File is uploaded successfully");
} else {
System.out.println("File uploading failed");
}
client.logout();
} catch (FTPConnectionClosedException e) {
e.printStackTrace();
} finally {
try {
client.disconnect();
} catch (FTPConnectionClosedException e) {
System.out.println(e);
}
}
}
}
jsp代码:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>File Uploading Form</title>
</head>
<body>
<h3>File Upload:</h3>
Select a file to upload: <br />
<form action="UploadServlet" method="post"
enctype="multipart/form-data">
<input type="file" name="file" size="50" />
<br />
<input type="submit" value="Upload File" />
</form>
</body>
</html>
谢谢