📄 downloadaction.java
字号:
/*
* Generated by MyEclipse Struts
* Template path: templates/java/JavaClass.vtl
*/
package org.adam.struts.action;
import java.io.UnsupportedEncodingException;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import org.adam.bean.Download;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.sql.DataSource;
import org.adam.bean.DB;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
/**
* MyEclipse Struts
* Creation date: 08-11-2008
*
* XDoclet definition:
* @struts.action validate="true"
*/
public class DownloadAction extends Action {
/*
* Generated Methods
*/
/**
* Method execute
* @param mapping
* @param form
* @param request
* @param response
* @return ActionForward
*/
String filename;
String path;
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) throws Exception{
// TODO Auto-generated method stub
request.setCharacterEncoding("GB2312");
int download_id=Integer.parseInt(request.getParameter("id"));
//System.out.println(download_id);
ServletContext context=servlet.getServletContext();
DataSource ds=(DataSource)context.getAttribute("download");
DB db=new DB(ds);
HttpSession session=request.getSession();
//filename=request.getParameter("filename");
filename=Download.getDownName(db, download_id);
//System.out.println(filename);
path = servlet.getServletContext().getRealPath("/fileupload")+"/"+filename;
//System.out.println(path);
downfile(response);
return null;
}
public void downfile(HttpServletResponse response) throws IOException{
String f;
if(filename!=null)
{
File file=new File(path);
if(!file.exists()) throw new IOException("文件不存在:"+file.getPath());
else{
f=toUTF8String(filename);
response.setContentType("application/x-msdownload");
response.setContentLength((int)file.length());
response.setHeader("Content-Disposition", "attachment;filename="+f);
write(response.getOutputStream());
}
}
}
public static String toUTF8String(String s){
StringBuffer sb=new StringBuffer();
for(int i=0;i<s.length();i++){
char c=s.charAt(i);
if(c>=0&&c<=255){
sb.append(c);
}
else{
byte[] b;
try{
b=Character.toString(c).getBytes("UTF-8");
}catch(Exception e){System.out.println(e);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();
}
protected void write(OutputStream os) throws IOException {
FileInputStream fis = new FileInputStream(path);
BufferedInputStream bis = new BufferedInputStream(fis);
byte[] s = new byte[1024];
int i = 0;
while ((i = bis.read(s)) > 0) {
os.write(s, 0, i);
}
if (os != null) {
os.flush();
os.close();
}
if (bis != null)
bis.close();
if (fis != null)
fis.close();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -