解决方案 »
- jgraphx不能显示中文的问题
- java动态页面转静态页面
- eclipse中middlegen-hibernate中的两个配置文件
- jsp页面加载时调用jquery ajax读取数据返回页面append()到一个div中可以正常显示,但是再调用onclick方法重新调用下这个方法就不行了
- session超时跳回登录页如何处理弹出框
- 哪位是英语高手??帮我看看Java2 SDK API中的帮助,本人英文很菜。。。大家帮忙
- 关于cmp的问题,环境jbuilder9,jboss3.22(进者有分,倾家荡产也得把这问题搞懂)
- ResultSet接口问题
- ■■这段JAVA代码看不懂,请解释一下谢谢■■
- 关于ejb发布的问题 大虾们请进
- hibernate 延迟加载问题
- web hibernate使用Jpa规范老是报错。
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream; import javax.servlet.http.HttpServletResponse; import org.apache.struts2.ServletActionContext; import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport; public class DownAction extends ActionSupport{ //要下载的文件名
private String filename;
//此方法封装完毕直接调用完成下载 f:要下载的文件 filename:下载的文件名
public void down(File f,String filename,HttpServletResponse response)
{
response.reset();
response.setContentType("image/jpeg");//设置下载文件的类型
response.setHeader("content-disposition","attachment; filename="+filename); //设置下载的文件名
long fileLength=f.length();
String length1=String.valueOf(fileLength);
response.setHeader("Content_Length",length1); //下载文件的大小
InputStream in=null;
OutputStream out = null;
try{
in = new FileInputStream( f );
out = response.getOutputStream();
byte[] buffer = new byte[2097152];
int ins = in.read(buffer);//读取字节到buffer中
//ins == -1 时 。就已经是文件的结尾了
while ( ins != -1 ) {
out.write(buffer, 0, ins);//将缓存buffer中的数据写到文件中
ins = in.read(buffer);
}
in.close();
out.flush();
out.close();
}catch (Exception e) {
System.out.println("--下载发生异常--");
try {
in.close();
out.flush();
out.close();
} catch (IOException e1) {
System.out.println("--关闭发生异常--");
in = null;
out = null;
e1.printStackTrace();
}
}
}
public void download()
{
ActionContext context=ActionContext.getContext();
//得到response对象
HttpServletResponse response=(HttpServletResponse)context.get(ServletActionContext.HTTP_RESPONSE);
//得到下载文件路径 upload文件夹下
String path=ServletActionContext.getRequest().getRealPath("/upload")+"\\"+this.getFilename();
File f = new File(path);
down(f,this.filename,response);
}
public String getFilename() {
return filename;
} public void setFilename(String filename) {
this.filename = filename;
}
}