import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.URLEncoder;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
@Path("/mail/attachment")
public class AttachmentResource {
/**
* 附件下载
* @param request
* @param response
* @param filename
* @return
*/
@GET
@Path("/download")
@Produces(MediaType.APPLICATION_OCTET_STREAM)//返回方式为流
public byte[] download( @Context HttpServletRequest request,@Context HttpServletResponse response,
@QueryParam("filename")String filename) {
System.out.println(filename);
FileInputStream fis = null;
try
{
fis = new FileInputStream(new File("e:\\email.sql"));
byte[] b = new byte[fis.available()];
fis.read(b);
response.setHeader("Content-Disposition","attachment;filename*=UTF-8''" + URLEncoder.encode(filename,"UTF-8"));//为文件命名,为防止文件名中文乱码,需要对文件名重编码
response.addHeader("content-type","application/pdf");
return b;
}
catch (FileNotFoundException e)
{
e.printStackTrace();
return null;
}
catch (IOException e)
{
e.printStackTrace();
return null;
}
}
}