package com.zjht.hrm.action;
import java.io.BufferedOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;
import org.apache.commons.codec.binary.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import nl.bitwalker.useragentutils.Browser;
import nl.bitwalker.useragentutils.UserAgent;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import com.zjht.hrm.dao.AttachmentDao;
import com.zjht.hrm.model.Attachment;
@Controller
public class AttachmentDownloadAction {
private AttachmentDao attachmentDao;
@RequestMapping(value="AttachmentDownload",method=RequestMethod.GET)
public void attachmentDownloadById(final long id, final boolean fail,
final HttpServletRequest request, final HttpServletResponse response) {
Attachment entity=null;
try {
entity=attachmentDao.findById(id);
if(entity==null){
try {
response.sendError(HttpServletResponse.SC_FORBIDDEN);
return;
} catch (IOException e) {
}
}
byte[] data=entity.getContent();
String fileName=entity.getFileName() == null ? "" : entity
.getFileName();
if (isImage(entity.getFileType()) && !fail) {
//imageDownload(request, response, entity);
PrintWriter out=response.getWriter();
out.println("
+"&fail=true\"/>");
out.flush();
out.close();
return;
}
response.reset();
response.setStatus(200);
FileType fileType=getFileType(entity.getFileType());
UserAgent userAgent=UserAgent.parseUserAgentString(request
.getHeader("user-agent"));
if (userAgent.getBrowser().getGroup() == Browser.IE)
fileName=java.net.URLEncoder.encode(fileName,"UTF-8");
else
fileName=newString(fileName.getBytes("UTF-8"),"ISO8859_1");
if(fileType==FileType.img ||fileType==FileType.pdf)
{
response.setHeader("Content-Disposition", "inline; filename=\""
+ fileName + "\"");
}else{
response.setHeader("Content-Disposition", "attachment; filename=\""
+ fileName + "\"");
}
response.addHeader("Content-Length", "" + data.length);
response.setHeader("Connection", "close");
response.setContentType(getContentType(entity.getFileType()));
OutputStream outputStream=newBufferedOutputStream(
response.getOutputStream());
outputStream.write(data);
outputStream.flush();
outputStream.close();
} catch (Exception e1) {
try {
response.sendError(HttpServletResponse.SC_FORBIDDEN);
} catch (IOException e) {
}
}
}
@SuppressWarnings("unused")
private void imageDownload(final HttpServletRequest request,
final HttpServletResponse response, Attachment entity)
throws IOException {
PrintWriter out=response.getWriter();
UserAgent userAgent=UserAgent.parseUserAgentString(request
.getHeader("user-agent"));
if (userAgent.getBrowser() == Browser.IE5
|| userAgent.getBrowser() == Browser.IE5_5
|| userAgent.getBrowser() == Browser.IE6
|| userAgent.getBrowser() == Browser.IE7) {
out.println("
+"&fail=true\"/>");
} else {
String content=Base64.encodeBase64String(entity.getContent());
out.println("
+ ";base64," + content + "\" />");
}
out.flush();
out.close();
}
public void setAttachmentDao(AttachmentDao attachmentDao) {
this.attachmentDao= attachmentDao;
}
private String getContentType(String extension) {
FileType fileType=null;
try {
fileType=Enum.valueOf(FileType.class, extension);
} catch (Exception e) {
return "application/octet-stream;charset=UTF-8";
}
switch (fileType) {
case xls:
// contentType设定
return "application/vnd.ms-excel;charset=utf-8";
case xlsx:
// contentType设定
return "application/vnd.ms-excel;charset=utf-8";
case pdf:
// contentType设定
return "application/pdf;charset=utf-8";
case doc:
// contentType设定
return "application/msword;charset=utf-8";
case docx:
// contentType设定
return "application/msword;charset=utf-8";
case txt:
// contentType设定
return "text/plain;charset=utf-8";
default:
// contentType设定
return "application/octet-stream;charset=UTF-8";
}
}
public enum FileType {
xls("xls"), xlsx("xlsx"), pdf("pdf"), doc("doc"), docx("docx"), txt(
"txt"),gif("gif"),jpeg("jpeg"),jpg("jpg"),png("png"),img("img");
FileType(String name) {
this.name= name;
}
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name= name;
}
}
private FileType getFileType(String extension){
FileType fileType=null;
try {
fileType=Enum.valueOf(FileType.class, extension);
} catch (Exception e) {
return null;
}
switch (fileType) {
case pdf:
return fileType;
case gif:
return FileType.img;
case jpeg:
return FileType.img;
case jpg:
return FileType.img;
case png:
return FileType.img;
default:
return null;
}
}
private boolean isImage(String fileType) {
if (fileType.equals("png") || fileType.equals("gif")
|| fileType.equals("jpg")) {
return true;
} else {
return false;
}
}
}