使用applet通过网页从服务器端下载文件

来源:互联网 发布:雅马哈 yas152 知乎 编辑:程序博客网 时间:2024/05/17 02:49

因为项目需要,舍弃了struts2方便的文件下载方式,采用了applet,主要为了下载的时候手动点出一个文件下载的选择框,在进行文件的下载保存。

下面开始介绍实现的过程:

1.applet代码

import java.applet.Applet;import java.awt.Color;import java.io.File;import java.io.FileOutputStream;import java.io.FileWriter;import java.io.IOException;import java.io.InputStream;import java.net.MalformedURLException;import java.net.URL;import java.net.URLConnection;import javax.xml.ws.Response;public class getFile extends Applet {String info;/** * 请求的服务器action方法:比如我请求的就是http://localhost:8080/restools/tsh/CobolAction! * download.do */String serverpath;/** * 指定的客户端文件的下载路径 */String localpath;/** * 服务器端的文件所在路径 */String filePath;/* * (non-Javadoc) 初始化applet *  * @see java.applet.Applet#init() */public void init() {URL url;URLConnection con;serverpath = getParameter("serverpath");localpath = getParameter("localpath");filePath = getParameter("filePath");try {// 打开连接,注意filePath参数的使用url = new URL(serverpath + "?filePath=" + filePath);con = url.openConnection();// 开始连接url的时候,会去调用服务器的download方法,将服务端被下载的文件以流的方式写入到网络中,可以参考我自己的serverpathcon.connect();
                        //客户端将按照本地的路径,将网络中的流内容写入到文件中File file = new File(localpath);if (!file.exists()) {file.createNewFile();}getInfo(con, file);} catch (MalformedURLException mfe) {System.out.println("URL form error!");} catch (IOException ioe) {System.out.println("IO Exception!");}}public void getInfo(URLConnection urlc, File file) {String txt = new String();InputStream is;FileOutputStream fWriter = null;int size=0;
public void getInfo(URLConnection urlc, File file) {String txt = new String();InputStream is;FileOutputStream fWriter = null;int size=0;try {is = urlc.getInputStream();byte[] b = new byte[1024];fWriter = new FileOutputStream(file);while ((size=is.read(b)) != -1) {if(size==1024){fWriter.write(b);}else{byte[] last = new byte[size];System.arraycopy(b, 0, last, 0, size);fWriter.write(last);}}is.close();} catch (IOException ioe) {System.out.println("IO Exception!");txt = new String("File read failed!");try {fWriter.write(txt.getBytes());} catch (IOException e) {}} finally {try {fWriter.flush();fWriter.close();} catch (IOException e) {}}}}服务器端代码。download代码public String download() {try {                           //获得页面上传过来的服务器端文件路径,将文件写入到网络中,这时applet将从中取到流中的内容,从而将内容写入到本地文件中filepath = request.getParameter("filePath");File file = new File(filepath);InputStream fiStream = new FileInputStream(file);byte[] b = new byte[1024];OutputStream ops = response.getOutputStream();int size ;while ((size = fiStream.read(b)) != -1) {if(size == 1024){ops.write(b);}else{byte[] last = new byte[size];System.arraycopy(b, 0, last, 0, size);ops.write(last);}}fiStream.close();} catch (FileNotFoundException e) {log.error(e.toString(), e);} catch (IOException e) {log.error(e.toString(), e);}return NONE;}jsp页面:注意需要将getfile这个applet文件,打成jar包,并签名,放在工程的webapp目录下,和web-inf同级。这里的参数codeBasePath依旧是服务器的路径。serverPath是客户端请求的action中的方法,localPath是客户端下载文件的路径,filePath是服务器端需要下载的文件名
<%@ page language="java" contentType="text/html; charset=UTF-8"    pageEncoding="UTF-8"%><!DOCTYPE html><html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"></head><body><applet code="getFile" codebase="${codeBasePath}" ARCHIVE = "getfile.jar"  width="0" height="0"><param name="serverpath"  id="serverpath" value="${serverPath}"/><param name="localpath" id="localpath" value="${localPath}"/><param name="filePath" id="filePath" value="${filepath}"/></applet></body></html>
总结下:也就是当你去请求action中的download方法的时候,action会将以上的参数传递给getfile这个applet,applet执行的过程中,又去调用action中的一个方法,将文件以流的形式传递给网络,applet继续执行,将流中的数据写入到客户端的文件当中。至此,用applet下载文件就算完成了!希望对大家有所帮助!提供参数的获得方法:String codeBasePath = request.getScheme() + "://"+ request.getServerName() + ":"+ request.getServerPort()+ request.getContextPath();这个方法将会取得工程的路径,比如:http://localhost:8080/restools