我需要通过javascript调用applet中的函数。
就是在init中初始化一个成员变量的线程实例,然后每次我通过javascript调用applet函数的时候启动这个线程,进行相应的操作,参数之类的需要作为成员变量存储,执行完操作后将这个线程休眠掉,下次重新唤醒。
但是我不知道该怎么写啊,我对线程一点都不了解。求求哪位高手帮帮忙吧!!!!
就是在init中初始化一个成员变量的线程实例,然后每次我通过javascript调用applet函数的时候启动这个线程,进行相应的操作,参数之类的需要作为成员变量存储,执行完操作后将这个线程休眠掉,下次重新唤醒。
但是我不知道该怎么写啊,我对线程一点都不了解。求求哪位高手帮帮忙吧!!!!
applet代码:
package applet;import java.applet.Applet;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;import javax.swing.ProgressMonitor;
import javax.swing.ProgressMonitorInputStream;public class Down extends Applet { private String filePath;
private String fileName;
private String url; private boolean bl=false;
public String getFilePath() {
return filePath;
} public void setFilePath(String filePath) {
this.filePath = filePath;
System.out.println(this.filePath);
} public String getFileName() {
return fileName;
} public void setFileName(String fileName) {
this.fileName = fileName;
System.out.println(this.fileName);
} public String getUrl() {
return url;
} public void setUrl(String url) {
this.url = url;
System.out.println(this.url);
} public void setBl(){
this.bl=true;
System.out.println(this.bl);
download();
}
public void init() { } public void download() {
try {
String filePath=getFilePath();
String fileName =getFileName();
String urlstring=getUrl(); System.out.println(filePath+"*****");
//网络路径很重要
URL url1 = new URL(urlstring+"?filePath="+filePath); //打开打开SOCKET链接
HttpURLConnection conn = (HttpURLConnection) url1
.openConnection();
//xinzeng
conn.disconnect();
conn.setRequestMethod("POST");
conn.setAllowUserInteraction(true);
conn.setDoInput(true);
conn.setDoOutput(true);
conn.setUseCaches(true);
conn.setRequestProperty("Content-Type", "application/octet-stream");
conn.connect(); //获取流
InputStream is=conn.getInputStream();
BufferedInputStream bis = new BufferedInputStream(is);
System.out.println("获取流成功*****:"+is.read()); //取文件类型
//fileName 完整的文件名
// String tr=fileName.substring(fileName.lastIndexOf("."),fileName.length());
File file = new File("E:/test/upload/"+fileName);
byte line[] = new byte[1024];
int a;
a = bis.read(line);
FileOutputStream outstream = new FileOutputStream(file);
BufferedOutputStream bos = new BufferedOutputStream(outstream);
while (a != -1) {
bos.write(line,0,a);
bos.flush();
a = bis.read(line);
}
//关闭流
bis.close();
bos.close();
is.close();
outstream.close();
} catch (IOException e) { }
}}servlet代码:package servlet;import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.Calendar;import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;import com.DB.DBconnect;public class DownFile extends HttpServlet { /**
* Constructor of the object.
*/
public DownFile() {
super();
} /**
* Destruction of the servlet. <br>
*/
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
} /**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException { response.setContentType("text/html");
PrintWriter out = response.getWriter();
out
.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
out.println("<HTML>");
out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>");
out.println(" <BODY>");
out.print(" This is ");
out.print(this.getClass());
out.println(", using the GET method");
out.println(" </BODY>");
out.println("</HTML>");
out.flush();
out.close();
} /**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to post.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException { // PrintWriter out=response.getWriter();
byte[] buff = new byte[1024];
boolean cont = true;
FileInputStream infile = null;
OutputStream fos=null;
String filePath=(String)request.getParameter("filePath");
File fe=new File(filePath);
//新增测试
/*
FileOutputStream fo=null;
String name=filePath.substring(filePath.lastIndexOf("/"),filePath.length());
String tr=name.substring(name.lastIndexOf("."),name.length());
// 根据时间得文件名
Calendar calendar = Calendar.getInstance();
String filename = String.valueOf(calendar.getTimeInMillis())
+tr;
File file=new File("D:/down/"+filename);
*/
//
try {
infile = new FileInputStream(fe);
//下载乱码
response.setContentType("application/OCTET-STREAM");
response.setHeader("Content-Disposition", "attachment; filename=\"" + filePath + "\"");
// fos = new FileOutputStream(fe);
//fo=new FileOutputStream(file);
fos=(OutputStream)response.getOutputStream();
} catch (FileNotFoundException e) {
System.err.println("没有找到文件");
System.exit(1);
} int n = infile.read(buff);
while (n!=-1) {
try {
// 从文件读取数据
fos.write(buff, 0, n); // 写入fos中
fos.flush();
// fo.write(buff, 0, n);//写入fo中
// fo.flush();//很重要
n=infile.read(buff);
} catch (Exception e) {
cont = false;
}
}
System.out.println("写入流成功!"); try {
infile.close();
fos.close();
// fo.close();
} catch (IOException e) {
System.err.println("文件错误");
System.exit(1);
}
//InputStream in = request.getInputStream();
//System.out.print("qqqq");
} /**
* Initialization of the servlet. <br>
*
* @throws ServletException if an error occurs
*/
public void init() throws ServletException {
// Put your code here
}}把applet打包,签名jsp页面代码:
<%@ page language="java" import="java.util.*,com.DB.DBconnect,java.sql.ResultSet" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>下载</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
<%
String url=request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+"/appUpFile/servlet/DownFile";
DBconnect dao=new DBconnect();
%>
</head>
<body>
服务器文件下载 <br>
<div>
<APPLET CODE = "applet.Down.class" archive="down.jar" mayscript height="1" width="1">
</APPLET>
<script type="text/javascript">
function tsb(n){
var fn=document.getElementById('fn'+n).value;
var fp=document.getElementById('fp'+n).value;
var ul='<%=url%>';
document.applets[0].setFileName(fn);
document.applets[0].setFilePath(fp);
document.applets[0].setUrl(ul);
document.applets[0].setBl();
}
</script>
</div>
<table>
<tr>
<td>文件名</td>
<td>路径</td>
<td>下载</td>
</tr>
<%
try{
int i=0;
ResultSet rs=dao.findAll();
while(rs.next()){
%>
<tr>
<td><input type="hidden" name="fn<%=i %>" id="fn<%=i %>" value="<%=rs.getString("name") %>"><%=rs.getString("name") %></td>
<td><input type="hidden" name="fp<%=i %>" id="fp<%=i %>" value="<%=rs.getString("filePath") %>"><%=rs.getString("filePath") %></td>
<td><input type="button" name="sb" value="下载" onclick="tsb('<%=i %>')">
</td>
</tr>
<%
//rs.getString("fileName");
i++;
}
}catch(Exception e){
e.printStackTrace();
}
%>
</table>
<%
dao.close();
%>
</body>
</html>测试后,报 java.security.AccessControlException: access denied 异常
打包,签名了,有证书啊。
从网上查了。说有两种方法。
1:就是在init中初始化一个成员变量的线程实例,然后每次我通过javascript调用applet函数的时候启动这个线程,进行相应的操作,参数之类的需要作为成员变量存储,执行完操作后将这个线程休眠掉,下次重新唤醒。
但是我不知道该怎么写啊,我对线程一点都不了解2.修改客户端的java.policy文件。不过这样的话,每台机子都要修改。