package
cn.com;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class Attachment 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 Attachment() ...{
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 ...{
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(GIF_TYPE);
imageStream.write(data);
}
if (file_type.equals("jpg")) ...{
response.setContentType(JPG_TYPE);
imageStream.write(data);
}
if (file_type.equals("bmp")) ...{
response.setContentType(BMP_TYPE);
imageStream.write(data);
}
if (file_type.equals("zip")) ...{
response.addHeader("Content-Disposition","attachment; filename="+file_name+".zip");
response.setContentType(ZIP_TYPE);
imageStream.write(data);
}
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);
}
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 ...{
}
}
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class Attachment 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 Attachment() ...{
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 ...{
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(GIF_TYPE);
imageStream.write(data);
}
if (file_type.equals("jpg")) ...{
response.setContentType(JPG_TYPE);
imageStream.write(data);
}
if (file_type.equals("bmp")) ...{
response.setContentType(BMP_TYPE);
imageStream.write(data);
}
if (file_type.equals("zip")) ...{
response.addHeader("Content-Disposition","attachment; filename="+file_name+".zip");
response.setContentType(ZIP_TYPE);
imageStream.write(data);
}
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);
}
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 ...{
}
}