struts2实现文件下载,但是下载的文件是损坏的,还有中文乱码问题怎么解决

w774254848 2013-02-14 06:35:01
。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。
...全文
246 4 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
idm2008 2013-02-16
  • 打赏
  • 举报
回复
雪山红柳 2013-02-16
  • 打赏
  • 举报
回复
雪山红柳 2013-02-16
  • 打赏
  • 举报
回复
package com; 

import java.io.File; 
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; 
        } 
     
     

}
雪山红柳 2013-02-16
  • 打赏
  • 举报
回复
可以解决你的问题

67,550

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧