package servlet;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import bean.WorkDownLoadBean;
/**
* Servlet implementation class WorkDownLoad
*/
public class WorkDownLoad extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public WorkDownLoad() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
WorkDownLoadBean download=new WorkDownLoadBean(request,response);
download.downLoad();
}
}
package bean;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import data.DataConn;
import data.Results;
public class WorkDownLoadBean {
private HttpServletResponse resp;
private HttpServletRequest req;
private File file=null;
private String name="";
public WorkDownLoadBean(HttpServletRequest request,
HttpServletResponse response) {
this.req=request;this.resp=response;
}
public void downLoad() {
getFile();
if(file.exists()){
FileInputStream fileInputStream=null;
try {
fileInputStream=new FileInputStream(file);
resp.setContentType("text/html");
ServletOutputStream out = resp.getOutputStream();
resp.setContentType("application/x-msdownload");
resp.setHeader("Content-Disposition", "attachment; filename=" + new String(name.getBytes("UTF-8"),"iso8859-1") + "");
if (fileInputStream != null) {
byte[] b = new byte[1024];
int i = 0;
while((i = fileInputStream.read(b)) > 0)
{
out.write(b, 0, i);
}
out.flush();
}
fileInputStream.close();
out.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
else{
System.out.println(file.getAbsolutePath() + " 文件不存在!");
return;
}
}
private void getFile(){
DataConn data=DataConn.getInstance();
String filepath=req.getRealPath("/");
int id=-1;
try {
String id_buf=new String(req.getParameter("id").getBytes("ISO8859_1"),"UTF-8").toString();
id=Integer.parseInt(id_buf.trim());
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
PreparedStatement call=null;
try {
call=data.getConnection().prepareStatement("call findworks(?)");
call.setInt(1, id);
ResultSet set=call.executeQuery();
while(set.next()){
filepath+=set.getString("link").substring(2).replace('/','\\');
name=set.getString("name");
}
System.out.print(filepath);
file=new File(filepath);
} catch (SQLException e) {
e.printStackTrace();
}
}
}
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import bean.WorkDownLoadBean;
/**
* Servlet implementation class WorkDownLoad
*/
public class WorkDownLoad extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public WorkDownLoad() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
WorkDownLoadBean download=new WorkDownLoadBean(request,response);
download.downLoad();
}
}
package bean;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import data.DataConn;
import data.Results;
public class WorkDownLoadBean {
private HttpServletResponse resp;
private HttpServletRequest req;
private File file=null;
private String name="";
public WorkDownLoadBean(HttpServletRequest request,
HttpServletResponse response) {
this.req=request;this.resp=response;
}
public void downLoad() {
getFile();
if(file.exists()){
FileInputStream fileInputStream=null;
try {
fileInputStream=new FileInputStream(file);
resp.setContentType("text/html");
ServletOutputStream out = resp.getOutputStream();
resp.setContentType("application/x-msdownload");
resp.setHeader("Content-Disposition", "attachment; filename=" + new String(name.getBytes("UTF-8"),"iso8859-1") + "");
if (fileInputStream != null) {
byte[] b = new byte[1024];
int i = 0;
while((i = fileInputStream.read(b)) > 0)
{
out.write(b, 0, i);
}
out.flush();
}
fileInputStream.close();
out.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
else{
System.out.println(file.getAbsolutePath() + " 文件不存在!");
return;
}
}
private void getFile(){
DataConn data=DataConn.getInstance();
String filepath=req.getRealPath("/");
int id=-1;
try {
String id_buf=new String(req.getParameter("id").getBytes("ISO8859_1"),"UTF-8").toString();
id=Integer.parseInt(id_buf.trim());
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
PreparedStatement call=null;
try {
call=data.getConnection().prepareStatement("call findworks(?)");
call.setInt(1, id);
ResultSet set=call.executeQuery();
while(set.next()){
filepath+=set.getString("link").substring(2).replace('/','\\');
name=set.getString("name");
}
System.out.print(filepath);
file=new File(filepath);
} catch (SQLException e) {
e.printStackTrace();
}
}
}