之前用flash.display.Loader,不支持bmp文件的预览。现在修改服务器java的代码,flex通过请求html来预览pdf以及所有图片格式
注意:因为是预览不是在线,所有Content-Disposition要设置成inline,不要设置成附件attachment
客户端flex的设置
var parm:Object = _showFileItem.getPostParam(false);
var showPicURL:URLRequest = new URLRequest();
var postData:URLVariables = new URLVariables();
postData.serverFileName = _showFileItem.serverFileName;
postData.serverFilePath = _showFileItem.serverFilePath;
showPicURL.data = postData;
showPicURL.url = parm["showPicUrl"]; //请求后天的服务 路径比如是http://192.168.1.100:8080/ZFS/showPicture.do
var filetype:String = _showFileItem.serverFileName.toString();
var fileFilt:String =filetype.substring(filetype.lastIndexOf(".")+1,filetype.length);
if(fileFilt=="pdf"||fileFilt=="gif" || fileFilt=="jpg" || fileFilt=="bmp" || fileFilt=="png"){
showPicURL.method=URLRequestMethod.POST;
navigateToURL(showPicURL,"_blank");
}else{
Alert.show("只支持图片格式!","提示");
}
服务器java代码如下:
package com.zhan.fileupload;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class ShowFile extends HttpServlet{
/**
*
*/
private static final long serialVersionUID = 3089316329737765177L;
public void doGet(HttpServletRequest request,HttpServletResponse response) throws IOException{
doPost(request,response);
}
public void doPost(HttpServletRequest request,HttpServletResponse response) throws IOException{
try {
downLoad(request,response);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private void downLoad(HttpServletRequest request,HttpServletResponse response) throws Exception {
// BufferedOutputStream bos = null;
// BufferedInputStream bis = null;
String serverFilePath = request.getParameter("serverFilePath");
if(serverFilePath!=null)
serverFilePath = new String(serverFilePath.getBytes("ISO8859-1"),"utf-8");
String serverFileName = request.getParameter("serverFileName");
if(serverFileName!=null)
serverFileName = new String(serverFileName.getBytes("ISO8859-1"),"utf-8");
System.out.print(serverFilePath+serverFileName);
String fileName = serverFilePath + serverFileName;
BufferedInputStream br = null;
OutputStream out = null;
//查看文档
try{
File f = new File(fileName);
System.out.println(f.getName());
if(!f.exists()){
response.sendError(404,"File not found!");
return;
}
br = new BufferedInputStream(new FileInputStream(f));
byte[] buf = new byte[1024];
int len = 0;
response.reset(); //非常重要
//文 件类型
if(serverFileName.toUpperCase().lastIndexOf(".pdf".toUpperCase())!=-1){
response.setContentType("application/pdf");
}
else if(serverFileName.toUpperCase().lastIndexOf(".doc".toUpperCase())!=-1
||
serverFileName.toUpperCase().lastIndexOf(".docx".toUpperCase())!=-1){
response.setContentType("application/msword");
}
else if(serverFileName.toUpperCase().lastIndexOf(".xls".toUpperCase())!=-1){
response.setContentType("application/vnd.ms-excel");
}else if(serverFileName.toUpperCase().lastIndexOf(".ppt".toUpperCase())!=-1){
response.setContentType("application/vnd.ms-powerpoint");
}else if(serverFileName.toUpperCase().lastIndexOf(".jpg".toUpperCase())!=-1
){
response.setContentType("image/jpg;");
}
else if(
serverFileName.toUpperCase().lastIndexOf(".png".toUpperCase())!=-1
){
response.setContentType("image/png;");
}
else if(
serverFileName.toUpperCase().lastIndexOf(".gif".toUpperCase())!=-1
){
response.setContentType("image/gif;");
}
else if(
serverFileName.toUpperCase().lastIndexOf(".bmp".toUpperCase())!=-1){
response.setContentType("image/bmp;");
}
response.setHeader("Content-Disposition", "inline; filename=" + new String(f.getName().getBytes("GBK"),"ISO8859-1"));
response.setHeader("Pragma", "No-cache");
response.setHeader("Cache-Control", "No-cache");
response.setDateHeader("Expires", 0);
out = response.getOutputStream();
while((len = br.read(buf)) >0){
out.write(buf,0,len);
}
out.flush();
}catch(Exception e){
e.printStackTrace();
}finally{
if(br!=null){
br.close();
}
if(out!=null){
out.close();
}
} }
}
之前的通过服务器返回字节流,flex的调用Image显示,下面是部分代码
private var stream:URLStream = new URLStream();
private var bytes:ByteArray = new ByteArray();
var showPicURL:URLRequest = new URLRequest();
var postData:URLVariables = new URLVariables();
postData.serverFileName = fileItem.serverFileName;
postData.serverFilePath = fileItem.serverFilePath;
showPicURL.data = postData;
showPicURL.url = parm["showPicUrl"]; //请求后天的服务 路径比如是http://192.168.1.100:8080/ZFS/showPicture.do
stream.addEventListener(Event.COMPLETE, onLoadComplete);
stream.load(showPicURL);
private function onLoadComplete(event:Event):void
{
if (stream.connected)
{
var b:int;
try {
do {
b = stream.readByte();
bytes.writeByte(b);
} while (true);
} catch (e:EOFError) {
//Alert.show(bytes.length.toString());
}
}
var loaderPic:Loader = new Loader();//Loader 类可用于加载 SWF 文件或图像(JPG、PNG 或 GIF)文件
loaderPic.loadBytes(bytes);
showImage.addChild(loaderPic); //showImage是flex的Image组件id
}
后台java文件是:
package com.zhan.fileupload;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.IOException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class ShowPic extends HttpServlet {
/**
*
*/
private static final long serialVersionUID = 3089316329737765177L;
public void doGet(HttpServletRequest request,HttpServletResponse response) throws IOException{
doPost(request,response);
}
public void doPost(HttpServletRequest request,HttpServletResponse response) throws IOException{
try {
downLoad(request,response);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private void downLoad(HttpServletRequest request,HttpServletResponse response) throws Exception {
BufferedOutputStream bos = null;
BufferedInputStream bis = null;
String serverFilePath = request.getParameter("serverFilePath");
if(serverFilePath!=null)
serverFilePath = new String(serverFilePath.getBytes("ISO8859-1"),"utf-8");
String serverFileName = request.getParameter("serverFileName");
if(serverFileName!=null)
serverFileName = new String(serverFileName.getBytes("ISO8859-1"),"utf-8");
System.out.print(serverFilePath+serverFileName);
String PicObject = serverFilePath + serverFileName;
try {
bis = new BufferedInputStream(new FileInputStream(PicObject));
bos = new BufferedOutputStream(response.getOutputStream());
byte[] buff = new byte[2048];
int bytesRead;
while(-1 != (bytesRead = bis.read(buff, 0, buff.length))) {
bos.write(buff,0,bytesRead);
}
} catch(final IOException e) {
e.printStackTrace();
} catch(Exception e) {
e.printStackTrace();
}finally {
if (bis != null)
bis.close();
if (bos != null)
{
bos.flush();
bos.close();
bos=null;
}
}
response.flushBuffer();
}
}