⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 download.java

📁 该HttpProxy用于从Applet或Swing界面中访问Ejb和服务端的JavaBean。
💻 JAVA
字号:
package com.nari.pmos.arch.servlet;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.naming.NamingException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.sql.DataSource;

public class Download extends HttpServlet {
	
	private static String str_path_temp = null;

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String downType = request.getParameter("downType");
		if (downType.equals("update")) {
			String pub_Id = request.getParameter("pub_Id");
			try {
				File file = new File(str_path_temp + pub_Id);
				FileInputStream ins = new FileInputStream(file);
				int length = (int) file.length();
				response.setContentType("application/octet-stream");
				response.setContentLength(length);
				response.addHeader("Content-Disposition",
						"attachment; filename=" + toGBKString(pub_Id));
				OutputStream out = response.getOutputStream();
				byte bytes[] = new byte[1024];
				for (int k = 0; k < length;) {
					int j = ins.read(bytes, 0, 1024);
					k += j;
					out.write(bytes, 0, j);
				}

				ins.close();
				out.flush();
				out.close();
			} catch (UnsupportedEncodingException ex) {
				System.err.println(ex);
			}
		} else {
			BigDecimal pub_Id = new BigDecimal(request.getParameter("pub_Id"));
			String fileName = null;
			try {

				javax.naming.Context initCtx = new javax.naming.InitialContext();
				Connection con = ((DataSource) initCtx
						.lookup("java:comp/env/jdbc/pmosDB")).getConnection();
				con.setAutoCommit(false);
				PreparedStatement pst = con
						.prepareStatement("SELECT FILE_NAME, ATTACHMENT FROM SYSTEM_PUBINFO WHERE PUB_ID =?");
				pst.setBigDecimal(1, pub_Id);
				ResultSet rs = pst.executeQuery();
				java.sql.Blob blob = null;
				if (rs.next()) {
					fileName = rs.getString("FILE_NAME");
					blob = rs.getBlob("ATTACHMENT");
				}

				int length = (int) blob.length();
				InputStream ins = blob.getBinaryStream();
				response.setContentType("application/octet-stream");
				response.setContentLength((int) length);
				response.addHeader("Content-Disposition",
						"attachment; filename=" + toGBKString(fileName));
				OutputStream out = response.getOutputStream();
				byte[] bytes = new byte[1024];
				int k = 0;
				while (k < length) {
					int j = ins.read(bytes, 0, 1024);
					k += j;
					out.write(bytes, 0, j);
				}
				ins.close();
				out.flush();
				out.close();
			} catch (java.io.UnsupportedEncodingException ex) {
				System.err.println(ex);
			} catch (SQLException e) {
				e.printStackTrace();
			} catch (NamingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doGet(request, response);
	}

	public static String toGBKString(String s) {
		StringBuffer sb = new StringBuffer();
		for (int i = 0; i < s.length(); i++) {
			char c = s.charAt(i);
			if (c >= 0 && c <= '\377') {
				sb.append(c);
			} else {
				byte b[];
				try {
					b = Character.toString(c).getBytes("GB2312");
				} catch (Exception ex) {
					System.out.println(ex);
					b = new byte[0];
				}
				for (int j = 0; j < b.length; j++) {
					int k = b[j];
					if (k < 0)
						k += 256;
					sb.append("%" + Integer.toHexString(k).toUpperCase());
				}
			}
		}
		return sb.toString();
	}

	@Override
	public void init() throws ServletException {
		// TODO Auto-generated method stub
		super.init();
		str_path_temp = getInitParameter("rootPath");
	}
	
	

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -