applet下载图片的实现

问题:
由于安全性的限制applet在向客户端读写文件的时候需要证书验证,而考虑到BS结构不方便给在所有用户上安装证书认证,所以产生了问题。

解决方案:
1. Applet生成文件流;
2. Applet将文件流通过url连接传给服务器端;
3. 服务器端将文件流保存成服务器端指定目录的文件,为了防止文件名冲突,以当前时间的微秒数new Date().getTime()命名文件;
4. 服务器端将文件名回传给Applet;
5. Applet调用java Script 打开新的浏览器页面调用下载指定文件的url;
6. 利用smartUpload实现服务器端指定文件名文件的下载。

部分代码示例:
--------------------------------------------------------------------------------------------------------------------------------------
Applet端代码;
--------------------------------------------------------------------------------------------------------------------------------------
[code]public void doOutPic(){
try{
Image image=null;
BufferedImage imageBuffer = graph.getImage(Color.WHITE,10);

URL url = new URL(getCodeBase(),"/unuba/dayActionGraphPicAction.do");
URLConnection con = url.openConnection();
con.setUseCaches(false);
con.setDoOutput(true);
con.setRequestProperty("Content-type", "application/octest-stream");
// 写入对象
OutputStream out = con.getOutputStream();
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
encoder.encode(imageBuffer);
out.flush();
out.close();
DataInputStream in = new DataInputStream(con.getInputStream());
ObjectInputStream objStream;
objStream = new ObjectInputStream(in);
String fileName = (String) objStream.readObject();
try {
//getAppletContext().showDocument(new URL("javascript:doAlert(\"" + fileName +"\")"));
URL destination=new URL(getCodeBase(),"/unuba/uba/downloadDayActionGraph.jsp?fileName="+fileName);
getAppletContext().showDocument(destination,"_blank");
}
catch (MalformedURLException me) {
me.printStackTrace();
}

}catch(Exception e){
e.printStackTrace();
}
}
[/code]
----------------------------------------------------------------------------------------------------------------------------------------
服务器端代码:
----------------------------------------------------------------------------------------------------------------------------------------
[code]
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {

//response.setContentType("application/octest-stream");

InputStream input=null;
FileOutputStream output=null;
byte[] buffer = new byte[1024];
try{
input=request.getInputStream();
Date now =new Date();
String fileName=now.getTime()+".jpg";
String path=request.getRealPath("/graphpic")+"/"+fileName;
this.outPutFileName(response,fileName);
output = new FileOutputStream(path);
int b;
while (true) {
if (input.available() < 1024) {
while ((b = input.read()) != -1) {
output.write(b);
}
break;
} else {
input.read(buffer);
output.write(buffer);
}
}
input.close();
//output.flush();
output.close();
}catch(Exception e) {
logger.error("can not get BufferedImage");
e.printStackTrace();
return null;
}

return null;

}

// output the graph picture file name
private void outPutFileName(HttpServletResponse response,String fileName){
try {
if (fileName == null) {
return;
}
OutputStream out;
ObjectOutputStream objStream;
out = response.getOutputStream();
objStream = new ObjectOutputStream(out);
objStream.writeObject((Object) fileName);

} catch (Exception e) {
logger.error("write output stream exception ", e);
}
}
[/code]
----------------------------------------------------------------------------------------------------------------------------------------
smartUpload下载文件的代码:
----------------------------------------------------------------------------------------------------------------------------------------
[code]
<%@ page contentType="text/html; charset=UTF-8" import="com.jspsmart.upload.*" %>
<%
// 新建一个SmartUpload对象
SmartUpload su = new SmartUpload();
// 初始化
su.initialize(pageContext);
// 设定contentDisposition为null以禁止浏览器自动打开文件,

//保证点击链接后是下载文件。
su.setContentDisposition(null);
// 下载文件
String fileName=request.getParameter("fileName");
if(fileName!=null){
su.downloadFile("/graphpic/"+fileName);
}
%>
[/code]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值