最近通過做的項(xiàng)目中修改個(gè)bug,在jsp頁面中潛入applet,通過applet來上傳5-10G之間的文件。
修改過程記錄如下:
JSP頁面中
<div id="appletTemp">
<applet code = "org/apache/commons/yp/MyApplet.class" name="test" archive="applet.jar" height="32" width="260" mayscript> <s:text name="image.add.installJdk"></s:text> </applet> </div>
以下為Java Applet結(jié)合commons-net-2.0.jar包實(shí)現(xiàn)上傳文件的功能代碼:
package org.apache.commons.yp;
import java.applet.Applet;
import java.awt.BorderLayout;
import java.awt.Font;
import java.awt.HeadlessException;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JTextField;
import javax.swing.filechooser.FileFilter;
import netscape.javascript.JSObject;
public class MyApplet extends Applet implements ActionListener {
public void destroy() {
}
public String getAppletInfo() {
return "This is my default applet created by Eclipse";
}
public void init() {
}
public void start() {
}
public void stop() {
}
private static final long serialVersionUID = 1L;
private MyApplet jFrame = null;
private JButton jFileButton = null;
private JButton onClickButton = null;
JTextField jtf = new JTextField(10);
File fileIn = null;
FileFtpApplet myFtp = new FileFtpApplet();
public int totalSize = 0;
public int nowSize = 0;
public String hostName;
public Integer port;
public String user;
public String password;
public String getHostName() {
return hostName;
}
public void setHostName(String hostName) {
this.hostName = hostName;
}
public Integer getPort() {
return port;
}
public void setPort(Integer port) {
this.port = port;
}
public String getUser() {
return user;
}
public void setUser(String user) {
this.user = user;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public MyApplet() {
System.out.println(" MyApplet start ");
jFrame = this;
jFrame.setSize(50, 10);
jFileButton = new JButton("瀏覽");
jFileButton.setFont(new Font("system", Font.PLAIN, 12));
jFileButton.setSize(2, 2);
onClickButton = new JButton("上傳");
onClickButton.setFont(new Font("system", Font.PLAIN, 12));
jFrame.add(jtf, BorderLayout.NORTH);
jFrame.add(jFileButton);
jFrame.add(onClickButton);
jtf.setEditable(true);
onClickButton.addActionListener((ActionListener) this);
jFileButton.addActionListener((ActionListener) this);
onClickButton.setSize(2, 20);
System.out.println(" MyApplet end ");
this.start();
}
public void actionPerformed(ActionEvent e) {
System.out.println(" actionPerformed(ActionEvent e) ");
String comm = e.getActionCommand();
try {
if ("瀏覽".equals(comm)) {
JFileChooser jfChooser = new JFileChooser("");
jfChooser.setDialogTitle("打開并上傳文件");
jfChooser.setFileFilter(new FileFilter() {
@Override
public boolean accept(File f) {
if (f.getName().endsWith("img") || f.isDirectory())
return true;
return false;
}
@Override
public String getDescription() {
return "鏡像文件(*.img)";
}
});
int result = jfChooser.showOpenDialog(jFrame);
if (result == JFileChooser.APPROVE_OPTION) {
fileIn = jfChooser.getSelectedFile();
jtf.setText(fileIn.getPath());
try {
JSObject.getWindow(this).eval(
"getinfo(" + "\"" + fileIn.getName() + "\""
+ ")");
} catch (Exception ea) {
ea.printStackTrace();
}
if (fileIn != null && fileIn.exists()) {
try {
long l1 = System.currentTimeMillis();
System.out.println(hostName+"----hostName");
System.out.println(port+"----port");
System.out.println(user+"-----user");
System.out.println(password+"---------password");
if (myFtp.connect(hostName, port, user, password)) {
myFtp.setLocal(fileIn.getPath());
myFtp.setFileName(fileIn.getName());
myFtp.setRemote("../../opt/nebula/images");
long l2 = System.currentTimeMillis();
System.out.println("end:" + l2);
System.out.println("remaining:" + (l2 - l1));
}
} catch (Exception e1) {
System.out.println("連接出錯(cuò) " + e1.getMessage());
}
}
}
} else if ("上傳".equals(comm)) {
try {
JSObject.getWindow(this).eval(
"javascript:showLoading(" + "\"" + fileIn.getName()
+ "\"" + ")");
} catch (Exception ea) {
ea.printStackTrace();
}
myFtp.upload(hostName, port, user, password);
try {
JSObject.getWindow(this).eval(
"javascript:uploadEnd(" + "\"" + fileIn.getName()
+ "\"" + ")");
} catch (Exception ea) {
ea.printStackTrace();
}
}
} catch (HeadlessException e1) {
e1.printStackTrace();
} finally {
try {
myFtp.disconnect();
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
}
package org.apache.commons.yp;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.RandomAccessFile;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
import org.apache.commons.net.ftp.FTPReply;
/**
*
* Function Description:FTP 鍔熻兘綾伙紝鍚玴ut鍜実et鏂規(guī)硶
*/
public class FileFtpApplet {
public static FTPClient ftp = new FTPClient();
public String remote;
public String fileName;
public String local;
public String getRemote() {
return remote;
}
public void setRemote(String remote) {
this.remote = remote;
}
public String getFileName() {
return fileName;
}
public void setFileName(String fileName) {
this.fileName = fileName;
}
public String getLocal() {
return local;
}
public void setLocal(String local) {
this.local = local;
}
public boolean upload(String hostname, Integer port, String username,
String password) {
try {
this.connect(hostname, port, username, password);
} catch (IOException e1) {
e1.printStackTrace();
}
ftp.enterLocalPassiveMode();
try {
ftp.setFileType(FTPClient.BINARY_FILE_TYPE);
} catch (IOException e) {
e.printStackTrace();
}
try {
ftp.changeWorkingDirectory("");
} catch (IOException e) {
e.printStackTrace();
}
boolean result;
try {
createDir(remote);
} catch (IOException e) {
e.printStackTrace();
}
boolean isExists = false;
long fileSize = 0;
FTPFile[] files = new FTPFile[10];
try {
files = ftp.listFiles();
} catch (IOException e) {
e.printStackTrace();
}
for (int i = 0; i < files.length; i++) {
FTPFile file = files[i];
if (fileName.equals(file.getName())) {
isExists = true;
fileSize = file.getSize();
break;
}
}
if (isExists) {
System.out.println("fileSize:" + fileSize);
File fLocal = new File(local);
OutputStream os;
try {
os = ftp.appendFileStream(fileName);
RandomAccessFile randomAccessFileLocal;
randomAccessFileLocal = new RandomAccessFile(fLocal, "r");
randomAccessFileLocal.seek(fileSize);
int len = 0;
byte[] bt = new byte[1024];
while ((len = randomAccessFileLocal.read(bt)) > 0) {
os.write(bt, 0, len);
}
randomAccessFileLocal.close();
os.close();
result = true;
} catch (IOException e) {
e.printStackTrace();
}
} else {
try {
File f = new File(local);
FileInputStream in = new FileInputStream(f);
result = ftp.storeFile(fileName, in);
in.close();
return result;
} catch (Exception e) {
e.printStackTrace();
}
}
return false;
}
/**
* 寤虹珛FTP榪炴帴
*/
public boolean connect(String hostname, int port, String username,
String password) throws IOException {
ftp.connect(hostname, port);
ftp.login(username, password);
if (!FTPReply.isPositiveCompletion(ftp.getReplyCode())) {
disconnect();
return false;
}
return true;
}
/**
* 鍏抽棴FTP榪炴帴
*/
public void disconnect() throws IOException {
if (ftp.isConnected()) {
ftp.disconnect();
}
}
public void createDir(String dir) throws IOException {
if (dir != null && dir.trim().length() > 0) {
String[] dirs = dir.split("/");
String workingDir = "";
for (int i = 0; i < dirs.length; i++) {
if (!existDirectory(workingDir, dirs[i])) {
ftp.makeDirectory(dirs[i]);
workingDir = dirs[i];
ftp.changeWorkingDirectory(dirs[i]);
} else {
workingDir = dirs[i];
ftp.changeWorkingDirectory(dirs[i]);
}
}
}
}
public boolean existDirectory(String path, String dirName)
throws IOException {
boolean flag = false;
FTPFile[] ftpFileArr = ftp.listFiles(path);
for (FTPFile ftpFile : ftpFileArr) {
if (ftpFile.isDirectory()
&& ftpFile.getName().equalsIgnoreCase(dirName)) {
flag = true;
break;
}
}
return flag;
}
}
訪問該jsp頁面,第一次會(huì)提示“請(qǐng)安裝jdk” ,那么就按照他所說的,去
http://www.java.com/下載jre進(jìn)行安裝。
(但是我自己服務(wù)器上已經(jīng)有jdk,為什么默認(rèn)找到里面的jre呢,在研究中。。。)
控制臺(tái)中打印的信息:
Java Plug-in 1.6.0_26
使用 JRE 版本 1.6.0_26-b03 Java HotSpot(TM) Client VM
用戶主目錄 = C:\Documents and Settings\server
----------------------------------------------------
c: 清除控制臺(tái)窗口
f: 終結(jié)在結(jié)束隊(duì)列上的對(duì)象
g: 垃圾收集
h: 顯示此幫助消息
l: 轉(zhuǎn)儲(chǔ)類載入程序列表
m: 打印內(nèi)存使用
o: 觸發(fā)日志記錄
q: 隱藏控制臺(tái)
r: 重新載入策略配置
s: 轉(zhuǎn)儲(chǔ)系統(tǒng)和部署屬性
t: 轉(zhuǎn)儲(chǔ)線程列表
v: 轉(zhuǎn)儲(chǔ)線程堆棧
x: 清除類載入程序高速緩存
0-5: 設(shè)置跟蹤級(jí)別為<n>
----------------------------------------------------
。。。。。。。。。。。。。。。。。。。。。。。。。。
安裝完成jre后,報(bào)錯(cuò) ClassNotFoundException找不到應(yīng)用中的org/apache/commons/yp/MyApplet.class文件.
因?yàn)?span style="color: #0000ff;">archive="applet.jar",默認(rèn)找的是工程WebRoot下面的See the line -:
applet.jar,所以需要將MyApplet.java打包為applet.jar中并放到WebRoot下。
訪問還是報(bào)錯(cuò)說找不到org/apache/commons/net/ftp/FTPClient.class類,我工程中的lib下已經(jīng)存在commons-net-2.0.jar,找的應(yīng)該是默認(rèn)在applet.jar中類,
所以我又將commons-net-2.0.jar
中所有的class文件放到了applet.jar中。在訪問不存在找不到類的問題了。
但是又出現(xiàn)了新的問題,說
Caused by: java.security.AccessControlException: access denied (java.io.FilePermission <<ALL FILES>> read)等相關(guān)的異常。
解決方案如下:
Edit and append the snippet below, to the end of the policy file (The typical location of your policy file is C:\Program Files\Java\jre1.5.0_16\lib\security\java.policy). If you are running a different JRE (i.e. 1.6), then the folder name 'jre1.5.0_16' will obviously reflect your version of the java runtime environment.
在你自己的jre\lib\security\java.policy文件中增加如下配置。對(duì)于文件的訪問許可。
grant codeBase "http://*/-" {
permission java.security.AllPermission;
permission java.io.FilePermission "<<ALL FILES>>", "read";
permission java.io.FilePermission "<<ALL FILES>>", "write";
};
經(jīng)過測(cè)試,以上配置可用。
java applet界面如下:

以上問題解決之后,點(diǎn)擊瀏覽,彈出框選擇文件,之后點(diǎn)擊上傳。
查看jre控制臺(tái)說連接不上ftp 21端口,Connected refused Exception,
那就說明linux的ftp 21端口沒有開啟。看如下解決方案:
1-ssh以root用戶身份登錄 登錄Linux系統(tǒng)后,
vi /etc/xinetd.d/gssftp
# default: off
# description: The kerberized FTP server accepts FTP connections \
# that can be authenticated with Kerberos 5.
service ftp
{
flags = REUSE
socket_type = stream
wait = no
user = root
server = /usr/kerberos/sbin/ftpd
server_args = -l -a
log_on_failure += USERID
disable = yes
}
將
server_args = -l -a 修改為 server_args = -l
disable = yes 修改 disable = no
然后執(zhí)行
[root@vm-container-16-5 sbin]# service xinetd restart
Stopping xinetd: [ OK ]
Starting xinetd: [ OK ]
OK!!!!
2- 執(zhí)行service xinetd restart如果出現(xiàn)
xinetd: unrecognized service
則可能說明安裝操作系統(tǒng)時(shí)默認(rèn)未安裝xinetd
通過yum -y install xinetd
[root@vm-container-16-5 sbin]# service xinetd restart
xinetd: unrecognized service
[root@vm-container-16-5 sbin]# yum -y install xinetd
Rocks-5.3 | 1.1 kB 00:00
Setting up Install Process
Resolving Dependencies
There are unfinished transactions remaining. You might consider running yum-complete-transaction first to finish them.
The program yum-complete-transaction is found in the yum-utils package.
--> Running transaction check
---> Package xinetd.x86_64 2:2.3.14-10.el5 set to be updated
--> Finished Dependency Resolution
Dependencies Resolved
==========================================================================================================================================================================================================
Package Arch Version Repository Size
==========================================================================================================================================================================================================
Installing:
xinetd x86_64 2:2.3.14-10.el5 Rocks-5.3 125 k
Transaction Summary
==========================================================================================================================================================================================================
Install 1 Package(s)
Update 0 Package(s)
Remove 0 Package(s)
Total download size: 125 k
Downloading Packages:
xinetd-2.3.14-10.el5.x86_64.rpm | 125 kB 00:00
Running rpm_check_debug
Running Transaction Test
Finished Transaction Test
Transaction Test Succeeded
Running Transaction
Installing : xinetd 1/1
Installed:
xinetd.x86_64 2:2.3.14-10.el5
Complete!
[root@vm-container-16-5 sbin]# service xinetd restart
Stopping xinetd: [FAILED]
Starting xinetd: [ OK ]
[root@vm-container-16-5 sbin]# service xinetd restart
Stopping xinetd: [ OK ]
Starting xinetd: [ OK ]
Linux下如果FTP訪問不了可以進(jìn)行如下設(shè)置修改:在終端中執(zhí)行 vi /etc/selinux/config,將其中的selinux=enforcing的enforcing改為disable。然后保存退出vi,執(zhí)行setenforce 0 或者重啟。就可以上傳了。
如果,你不是上述的解決方法的話,有可能是你沒有到ftp服務(wù)器下的incoming文件夾中,或者你沒有把incoming文件夾的權(quán)限改為777。
ELinux是「Security-Enhanced Linux」的簡稱,是
美國國家安全局「NSA=The National Security Agency」 和SCC(Secure Computing Corporation)開發(fā)的 Linux的一個(gè)擴(kuò)張強(qiáng)制訪問控制安全模塊。原先是在Fluke上開發(fā)的,2000年以 GNU GPL 發(fā)布。
posted on 2011-06-21 15:07
David1228 閱讀(2255)
評(píng)論(0) 編輯 收藏 所屬分類:
Linux