当前位置: 技术问答>java相关
书上说servelet可以根据需要返回图片给浏览器,如何做啊
来源: 互联网 发布时间:2015-10-18
本文导语: 好像是先生成image流,然后呢 | Acquiring a Binary Stream for the Response Suppose you want to open a binary file in a browser from a servlet. It isn't text so you have to write the file to the servlet's output ...
好像是先生成image流,然后呢
|
Acquiring a Binary Stream for the Response
Suppose you want to open a binary file in a browser from a servlet. It isn't text so you have to write the file to the servlet's output stream. Let's practice with a PDF document. First, you get the servlet's output stream with:
ServletOutputStream out = res.getOutputStream();
Next, you set the file type in the response object using one of the standard MIME (Multipurpose Internet Mail Extension) protocols. Several listings of content type names are available on the Internet including one at ftp://ftp.isi.edu/in-notes/iana/assignments/media-types. Then you use an HTTP response header named content-disposition. This header allows the servlet to specify information about the file's presentation. Using that header, you can indicate that the content should be opened separately (not actually in the browser) and that it should not be displayed automatically, but rather upon some further action by the user. You can also suggest the filename to be used if the content is to be saved to a file. That filename would be the name of the file that appears in the Save As dialog box. If you don't specify the filename, you are likely to get the name of your servlet in that box. To find out more about the content-disposition header, check out Resources or go to http://www.alternic.org/rfcs/rfc2100/rfc2183.txt.
Sending a binary stream to the client is not easy. Listing 4.10 will help you do it right.
Listing 4.10 Servlet That Sends a File to the Client
public class BinaryResponse extends HttpServlet {
/**Set global variables*/
public void init(ServletConfig config)
throws ServletException
{
super.init(config);
}
/**Process HTTP Post request with doPost*/
public void doPost(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException
{
String fileName = "index.html"; //set file name
String contentType = getContentType(fileName);
//contentType = getType(); //get the content type
// get the file
File file = new File(fileName);
long length = file.length();
if(length > Integer.MAX_VALUE)
{
//handle too large file error
//perhaps log and return error message to client
}
byte[] bytes = new byte[(long)length];
BufferedInputStream in =
new BufferedInputStream(new FileInputStream(file));
// then place it into a byte array
if(length != in.read(bytes))
{
//handle too large file error
//perhaps log and return error message to client
}
//get the servlet's output stream
BufferedOutputStream out =
new BufferedOutputStream(response.getOutputStream());
//set the content type of the response
response.setContentType( contentType );
//send the file to the client
out.write( bytes );
}
}
/**Clean up resources*/
public void destroy()
{
//If you need to clean up resources.
//Otherwise don't override.
}
String getContentType(String fileName)
{
String extension[] =
{ // File Extensions
"txt", //0 - plain text
"htm", //1 - hypertext
"jpg", //2 - JPEG image
"gif", //3 - gif image
"pdf", //4 - adobe pdf
"doc", //5 - Microsoft Word
}, // you can add more
mimeType[] =
{ // mime types
"text/plain", //0 - plain text
"text/html", //1 - hypertext
"image/jpg", //2 - image
"image/gif", //3 - image
"application/pdf", //4 - Adobe pdf
"application/msword", //5 - Microsoft Word
}, // you can add more
contentType = "text/html"; // default type
// dot + file extension
int dotPosition = fileName.lastIndexOf('.');
// get file extension
String fileExtension =
fileName.substring(dotPosition + 1);
// match mime type to extension
for(int index = 0; index
Suppose you want to open a binary file in a browser from a servlet. It isn't text so you have to write the file to the servlet's output stream. Let's practice with a PDF document. First, you get the servlet's output stream with:
ServletOutputStream out = res.getOutputStream();
Next, you set the file type in the response object using one of the standard MIME (Multipurpose Internet Mail Extension) protocols. Several listings of content type names are available on the Internet including one at ftp://ftp.isi.edu/in-notes/iana/assignments/media-types. Then you use an HTTP response header named content-disposition. This header allows the servlet to specify information about the file's presentation. Using that header, you can indicate that the content should be opened separately (not actually in the browser) and that it should not be displayed automatically, but rather upon some further action by the user. You can also suggest the filename to be used if the content is to be saved to a file. That filename would be the name of the file that appears in the Save As dialog box. If you don't specify the filename, you are likely to get the name of your servlet in that box. To find out more about the content-disposition header, check out Resources or go to http://www.alternic.org/rfcs/rfc2100/rfc2183.txt.
Sending a binary stream to the client is not easy. Listing 4.10 will help you do it right.
Listing 4.10 Servlet That Sends a File to the Client
public class BinaryResponse extends HttpServlet {
/**Set global variables*/
public void init(ServletConfig config)
throws ServletException
{
super.init(config);
}
/**Process HTTP Post request with doPost*/
public void doPost(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException
{
String fileName = "index.html"; //set file name
String contentType = getContentType(fileName);
//contentType = getType(); //get the content type
// get the file
File file = new File(fileName);
long length = file.length();
if(length > Integer.MAX_VALUE)
{
//handle too large file error
//perhaps log and return error message to client
}
byte[] bytes = new byte[(long)length];
BufferedInputStream in =
new BufferedInputStream(new FileInputStream(file));
// then place it into a byte array
if(length != in.read(bytes))
{
//handle too large file error
//perhaps log and return error message to client
}
//get the servlet's output stream
BufferedOutputStream out =
new BufferedOutputStream(response.getOutputStream());
//set the content type of the response
response.setContentType( contentType );
//send the file to the client
out.write( bytes );
}
}
/**Clean up resources*/
public void destroy()
{
//If you need to clean up resources.
//Otherwise don't override.
}
String getContentType(String fileName)
{
String extension[] =
{ // File Extensions
"txt", //0 - plain text
"htm", //1 - hypertext
"jpg", //2 - JPEG image
"gif", //3 - gif image
"pdf", //4 - adobe pdf
"doc", //5 - Microsoft Word
}, // you can add more
mimeType[] =
{ // mime types
"text/plain", //0 - plain text
"text/html", //1 - hypertext
"image/jpg", //2 - image
"image/gif", //3 - image
"application/pdf", //4 - Adobe pdf
"application/msword", //5 - Microsoft Word
}, // you can add more
contentType = "text/html"; // default type
// dot + file extension
int dotPosition = fileName.lastIndexOf('.');
// get file extension
String fileExtension =
fileName.substring(dotPosition + 1);
// match mime type to extension
for(int index = 0; index
您可能感兴趣的文章:
本站(WWW.)旨在分享和传播互联网科技相关的资讯和技术,将尽最大努力为读者提供更好的信息聚合和浏览方式。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。
站内导航:
特别声明:169IT网站部分信息来自互联网,如果侵犯您的权利,请及时告知,本站将立即删除!