Ajax从服务端读取图片数据

Ajax从服务端读取图片数据

服务端代码:
@Action(value  =   " /imageload " )
    
public  String imageLoad () throws  Exception {
        
        File f 
=   new  File ( " d:/down.jpg " );
        FileInputStream in 
=   new  FileInputStream (f);
        
byte [] b  =   new   byte [in.available()];
        in.read(b);
        HttpServletResponse response 
=  ServletActionContext.getResponse();
//         response.setCharacterEncoding("UTF-8");
        response.setContentType( " image " );
        OutputStream pwt 
=  response.getOutputStream();
        pwt.write(b);
        pwt.flush();
        pwt.close();
        
return   null ;
    }
客户端代码:
<% @ page language = " java "  contentType = " text/html; charset=utf-8 "
    pageEncoding
= " utf-8 " %>
<! DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd" >
< html >
< head >
< meta  http-equiv ="Content-Type"  content ="text/html; charset=utf-8" >
< title > Insert title here </ title >
< script  type ="text/javascript"  src ="js/jquery-1.4.2.min.js" ></ script >
</ head >
< body >
< img  id ="img1"  src ="" ></ img >
< script  type ="text/javascript" >
    
function  load() {
        $('#img1').attr('src', 'subject
/ imageload.action');
    }
</ script >

< button  onclick =load(); > 点击获取 </ button >
</ body >
</ html >


你可能感兴趣的:(Ajax从服务端读取图片数据)