byte[] 文件输出 更新版 : )

package  cn.com. * . *
import  java.awt.geom.AffineTransform;
import  java.awt.image.AffineTransformOp;
import  java.awt.image.BufferedImage;
import  java.io.ByteArrayInputStream;
import  java.io.ByteArrayOutputStream;
import  java.io.File;
import  java.io.IOException;
import  java.util.List;

import  javax.imageio.ImageIO;
import  javax.servlet.ServletException;
import  javax.servlet.ServletOutputStream;
import  javax.servlet.http.HttpServlet;
import  javax.servlet.http.HttpServletRequest;
import  javax.servlet.http.HttpServletResponse;

import  cn.com.articles.action.DoArticles;
import  cn.com.articles.attachment.bean.AttachmentBean;
import  cn.com.util.Jiance;

public   class  Articles  extends  HttpServlet  {

    
private static final String JPG_TYPE = "image/jpeg";

    
private static final String GIF_TYPE = "image/gif";

    
private static final String BMP_TYPE = "image/bmp";

    
private static final String DOC_TYPE = "application/msword";

    
private static final String ZIP_TYPE = "application/octet-stream";

    
private static final String PDF_TYPE = "application/pdf";

    
public Articles() {
        
super();
    }


    
/**
     * Destruction of the servlet. <br>
     
*/

    
public void destroy() {
        
super.destroy(); // Just puts "destroy" string in log
    }


    
public void doGet(HttpServletRequest request, HttpServletResponse response)
            
throws ServletException, IOException {
        Jiance jc 
= new Jiance();
        
int temp = jc.done(request.getParameter("vid"));
        
byte[] data = null;
        String file_type 
= null;
        String file_name 
= null;
        DoArticles da 
= new DoArticles();
        List al 
= da.get_attachment(temp);
        
for (int i = 0; i < al.size(); i++{
            AttachmentBean attbean 
= (AttachmentBean) al.get(i);
            data 
= attbean.getFile_entity();
            file_type 
= attbean.getFile_ext();
            file_name 
= attbean.getFile_name();

        }

        ByteArrayOutputStream imageStream 
= new ByteArrayOutputStream();
        
if (file_type.equals("gif")) {
            response.setContentType(JPG_TYPE);
            BufferedImage bi 
= convert(data);
            ServletOutputStream streamOut 
= response.getOutputStream();
            ImageIO.write(bi, 
"jpeg", streamOut);
            streamOut.close();
        }

        
if (file_type.equals("jpg")) {
            response.setContentType(JPG_TYPE);
            BufferedImage bi 
= convert(data);
            ServletOutputStream streamOut 
= response.getOutputStream();
            ImageIO.write(bi, 
"jpeg", streamOut);
            streamOut.close();
        }

        
if (file_type.equals("bmp")) {
            response.setContentType(BMP_TYPE);
            BufferedImage bi 
= convert(data);
            ServletOutputStream streamOut 
= response.getOutputStream();
            ImageIO.write(bi, 
"BMP", streamOut);
            streamOut.close();
        }

        
if (file_type.equals("zip")) {
            response.addHeader(
"Content-Disposition""attachment; filename="
                    
+ file_name + ".zip");
            response.setContentType(ZIP_TYPE);
            imageStream.write(data);
            ServletOutputStream streamOut 
= response.getOutputStream();
            imageStream.writeTo(streamOut);
            streamOut.close();
        }

        
if (file_type.equals("doc")) {
            
//response.setHeader("Content-disposition", "inline; filename="
                    
//+ file_name + ".doc"); // 线上浏览方式
            response.addHeader("Content-Disposition""attachment; filename="
                    
+ file_name + ".doc");
            response.setContentType(DOC_TYPE);
            imageStream.write(data);
            ServletOutputStream streamOut 
= response.getOutputStream();
            imageStream.writeTo(streamOut);
            streamOut.close();
        }

        
if (file_type.equals("pdf")) {
            response.addHeader(
"Content-Disposition""attachment; filename="
                    
+ file_name + ".pdf");
            response.setContentType(PDF_TYPE);
            imageStream.write(data);
            ServletOutputStream streamOut 
= response.getOutputStream();
            imageStream.writeTo(streamOut);
            streamOut.close();
        }


    }


    
public void init() throws ServletException {
    }


    
public BufferedImage convert(byte[] data) {
        
try {
            ByteArrayInputStream bais 
= null;
            bais 
= new ByteArrayInputStream(data);
            
int newHeight = 0;
            
int newWidth = 0;

            
int nw = 140// 定义宽为140
            int nh = 100// 定义高为100
            AffineTransform transform = new AffineTransform();
            BufferedImage bis 
= ImageIO.read(bais);
            
int w = bis.getWidth(); // 原始宽度
            int h = bis.getHeight(); // 原始高度

            
double sx = (double) nw / w;
            
double sy = (double) nh / h;

            
int wideth = bis.getWidth(null);
            
int height = bis.getHeight(null);
            
if (wideth > height) // newWidth is the width
                newWidth = nw;
                newHeight 
= height * nw / wideth;
            }
 else {
                
int heightNew = nw;
                newWidth 
= nh * wideth / height;
                newHeight 
= nh;
            }


            
// 判断是横向图形还是坚向图形
            if (w > h) // 横向图形
            {
                
if ((int) (sx * h) > nh) // 比较高不符合高度要求,就按高度比例
                {
                    sx 
= sy;
                    nw 
= (int) (w * sx);
                }
 else {
                    sy 
= sx;
                    nh 
= (int) (h * sy);
                }

            }
 else {
                
if ((int) (sy * w) > nw) {
                    sy 
= sx;
                    nh 
= (int) (h * sy);
                }
 else {
                    sx 
= sy;
                    nw 
= (int) (w * sx);
                }

            }

            transform.setToScale(sx, sy);
            AffineTransformOp ato 
= new AffineTransformOp(transform, null);
            BufferedImage bid 
= new BufferedImage(nw, nh,
                    BufferedImage.TYPE_3BYTE_BGR);
            ato.filter(bis, bid);
            
return bid;
            
// ImageIO.write(bid, "jpeg", fo);
        }
 catch (Exception e) {
            e.printStackTrace();
        }

        
return null;

    }

}

 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值