日期:2014-05-17  浏览次数:20830 次

Applet 文件上传进度条问题
在applet中用HttpURLConnection 来上传文件,
  现在出现一个问题,如果在eclipse中运行applet上传插件,连接tomcat上传没问题,进度条也能实现,但如果把applet放到jsp页面中,在浏览器中访问,上传就出错,进度条也有问题,
  大概的问题是sessionID的原因。在用HttpURLConnection 进行上传的时候,我都会手动设置sessionID,保证同一会话,在session中会放置一个对象来保存上传的进度,applet也会有个请求不断的去访问服务器去查询这个对象,再返回上传的进度。但现在这个请求过去发现对象是空的,也就是说是个新的session,不知道为何。
  在eclipse中单独运行applet,没问题。

获取SESSIONID的方法,在JSP页面中时,SESSIONID会通过jsp页面传过来
Java code

    public static String beginConnection(String url) throws Exception{
            String str = null;
            HttpURLConnection conn = (HttpURLConnection) new URL(url)
                    .openConnection();            
            conn.setDoOutput(true);
            conn.setDoInput(true);
            conn.setUseCaches(false);
            conn.setRequestMethod("POST");
            conn.setInstanceFollowRedirects(false);
            conn.connect();
            conn.getInputStream();
            str = conn.getHeaderField("Set-Cookie");
            if(str != null){
                String[] session = str.split(";");
                str = session[0].substring(session[0].indexOf("=") + 1);
            }
            System.out.println("sessionID-->" + str);
            return str;
    }




查询进度方法
Java code

    public static String getProgress(String server, String sessionID) {
        String line = "";
        try {
            URL url = new URL(server);
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setDoOutput(true);
            conn.setDoInput(true);
            conn.setUseCaches(false);
            conn.setRequestMethod("POST");
            conn.setRequestProperty("Cookie", "JSESSIONID=" + sessionID);
            DataOutputStream out = new DataOutputStream(conn.getOutputStream());
            out.write(("folder=").getBytes("UTF-8"));
            out.flush();
            out.close();
            BufferedReader reader = new BufferedReader(new InputStreamReader(
                    conn.getInputStream(), "UTF-8"));
            StringBuffer buffer = new StringBuffer();
            while ((line = reader.readLine()) != null) {
                buffer.append(line);
            }
            reader.close();
            return buffer.toString();
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }



文件上传的方法
Java code

public static String upLoadFile(String server, File file, String sessionID,
            String folder) {
        System.out.println("upload sessionid-"+sessionID);
        String BOUNDARY = "---------7d4a6d158c954336agamacomcn"; // 定义数据分隔线
        try {
            URL url = new URL(server);
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            // 发送POST请求必须设置如下两行
            conn.setDoOutput(true);
            conn.setDoInput(true);
            conn.setUseCaches(false);
            conn.setRequestMethod("POST");
            conn.setRequestProperty("Connection", "Keep-Alive");
            conn.setRequestProperty("Charset", "UTF-8");
            conn.setRequestProperty("Cookie", "JSESSIONID=" + sessionID);
            conn.setRequestProperty("Content-Type","multipart/form-data; boundary=" + BOUNDARY);
            conn.setChunkedStreamingMode(BUFFER_SIZE);
。。