论坛首页 Java企业应用论坛

applet下载图片的实现

浏览 3707 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2007-08-16  
问题:
由于安全性的限制applet在向客户端读写文件的时候需要证书验证,而考虑到BS结构不方便给在所有用户上安装证书认证,所以产生了问题。

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

部分代码示例:
--------------------------------------------------------------------------------------------------------------------------------------
Applet端代码;
--------------------------------------------------------------------------------------------------------------------------------------
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();
		}
	}

----------------------------------------------------------------------------------------------------------------------------------------
服务器端代码:
----------------------------------------------------------------------------------------------------------------------------------------
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);
		}
	}

----------------------------------------------------------------------------------------------------------------------------------------
smartUpload下载文件的代码:
----------------------------------------------------------------------------------------------------------------------------------------
<%@ 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);
	}
%>

  • jspsmartupload.jar (11.6 KB)
  • 描述: 上传下载文件用到的jar包
  • 下载次数: 126
论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics