- 浏览: 159198 次
- 性别:
- 来自: 杭州
最新评论
由response.setContentType()方法开始谈JSP/Servelt上传下载文件
- 文章非原创,参考链接见文末!
- 常见的MIME类型如下表:
序号 |
内容类型 |
文件扩展名 |
描述 |
1 |
application/msword |
doc |
Microsoft Word |
2 |
application/octet-stream bin |
dms lha lzh exe class |
可执行程序 |
3 |
application/pdf |
|
Adobe Acrobat |
4 |
application/postscript |
ai eps ps |
PostScript |
5 |
appication/powerpoint |
ppt |
Microsoft Powerpoint |
6 |
appication/rtf |
rtf |
rtf 格式 |
7 |
appication/x-compress |
z |
unix 压缩文件 |
8 |
application/x-gzip |
gz |
gzip |
9 |
application/x-gtar |
gtar |
tar 文档 (gnu 格式 ) |
10 |
application/x-shockwave-flash |
swf |
MacroMedia Flash |
11 |
application/x-tar |
tar |
tar(4.3BSD) |
12 |
application/zip |
zip |
winzip |
13 |
audio/basic |
au snd |
sun/next 声音文件 |
14 |
audio/mpeg |
mpeg mp2 |
Mpeg 声音文件 |
15 |
audio/x-aiff |
mid midi rmf |
Midi 格式 |
16 |
audio/x-pn-realaudio |
ram ra |
Real Audio 声音 |
17 |
audio/x-pn-realaudio-plugin |
rpm |
Real Audio 插件 |
18 |
audio/x-wav |
wav |
Microsoft Windows 声音 |
19 |
image/cgm |
cgm |
计算机图形元文件 |
20 |
image/gif |
gif |
COMPUSERVE GIF 图像 |
21 |
image/jpeg |
jpeg jpg jpe |
JPEG 图像 |
22 |
image/png |
png |
PNG 图像 |
text/html HTML
text/plain TXT
text/xml XML
text/json json字符串
response.setContentType("text/plain"); <option value="image/bmp">BMP</option> <option value="image/gif">GIF</option> <option value="image/jpeg">JPEG</option> <option value="image/tiff">TIFF</option> <option value="image/x-dcx">DCX</option> <option value="image/x-pcx">PCX</option> <option value="text/html">HTML</option> <option value="text/plain">TXT</option> <option value="text/xml">XML</option> <option value="application/afp">AFP</option> <option value="application/pdf">PDF</option> <option value="application/rtf">RTF</option> <option value="application/msword">MSWORD</option> <option value="application/vnd.ms-excel">MSEXCEL</option> <option value="application/vnd.ms-powerpoint">MSPOWERPOINT</option> <option value="application/wordperfect5.1">WORDPERFECT</option> <option value="application/vnd.lotus-wordpro">WORDPRO</option> <option value="application/vnd.visio">VISIO</option> <option value="application/vnd.framemaker">FRAMEMAKER</option> <option value="application/vnd.lotus-1-2-3">LOTUS123</option>
此外不同浏览器下对同一个文件上传后获取到的类型可能不同。
1、文件下载:
文件下载的关键代码在于:
- < html >
- < head >
- < meta http-equiv = "Content-Type" content = "text/html; charset=GB18030" >
- < title > download page </ title >
- </ head >
- < body >
- < a href = "loadFile?filename=test.txt&path=" +escape("C:/test.txt") > Download It! </ a >
- </ body >
- </ html >
LoadFile类如下:
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.IOException;
- import java.io.OutputStream;
- import javax.servlet.ServletException;
- import javax.servlet.http.HttpServlet;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- public class LoadFile extends HttpServlet {
- public void doGet(HttpServletRequest request, HttpServletResponse response)
- throws IOException, ServletException {
- String filename = request.getParameter( "filename" );
- String path = request.getParameter( "path" );
- OutputStream o = response.getOutputStream();
- byte b[] = new byte [ 1024 ];
- // the file to download.
- File fileLoad = new File(path);
- // the dialogbox of download file.
- response.setHeader("Content-disposition" , "attachment;filename="
- + "test.txt" );
- // set the MIME type.
- response.setContentType("text/html" );
- // get the file length.
- long fileLength = fileLoad.length();
- String length = String.valueOf(fileLength);
- response.setHeader("Content_Length" , length);
- // download the file.
- FileInputStream in = new FileInputStream(fileLoad);
- int n = 0 ;
- while ((n = in.read(b)) != - 1 ) {
- o.write(b, 0 , n);
- }
- }
- public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
- doGet(request, response);
- }
- }
- test.jsp
- <%@page contentType= "text/html;charset=GB2312" %>
- <html>
- <body>
- 选择要上传的文件:<br />
- <form action="accept.jsp" method= "post" enctype= "multipart/form-data" >
- <input type="file" name= "boy" size= "38" >
- <br />
- <input type="hidden" id= "tt" name= "t" value= "1" />
- <input type="submit" id= "gg" name= "g" value= "提交" />
- </form>
- </body>
- </html>
- accept.jsp
- <%@page contentType= "text/html;charset=GB2312" %>
- <%@ page import = "java.io.*" %>
- <HTML>
- <BODY>
- <%
- //经测试,说明:ServletInputStream类中的readLine(byte[] b, int off, int len)
- //其中参数 byte[] b 起缓冲作用,此方法一次读取一行,但如果 byte[] b 定义的大小,比要读取的一行需占用的空间要小,则
- //该方法只读取 byte[] b 指定的大小;再次读取时会继续接着上次未读完的读取;返回值 :返回实际读取的字节数,当读到文档流的
- //最后时返回-1。
- try {
- ServletInputStream in=request.getInputStream();
- File f=new File( "c:\\test" , "a.txt" );
- FileOutputStream o=new FileOutputStream(f);
- //如果byte b[]=设置的值太短的话(假如设置为2),那么在 应用的 上传操作时会有影响,具体表现为无法解析文档路径等相关信息
- byte b[]= new byte [ 2046 ];
- int n;
- int i = 0 ;
- while ((n=in.readLine(b, 0 ,b.length))!=- 1 ) //ServletInputStream.readLine方法是逐行读取的。当它读完整个文件,返回-1,一般情况下返回读取的字节数
- {
- i++;
- System.out.println("------" +i);
- o.write(b,0 ,n);
- }
- o.close();
- in.close();
- }catch (IOException e){
- e.printStackTrace();
- }
- out.print("文件已经上传" );
- %>
- <a href="c:\\test\\a.txt" >查看结果</a>
- </body>
- </HTML>
- -----------------------------7db2611a404a4
- Content-Disposition: form-data; name = "boy" ; filename = "C:\Users\xijiang\Desktop\test.txt"
- Content-Type: text/plain
- Hello World!
- Hello World!
- Hello World!
- -----------------------------7db2611a404a4
- Content-Disposition: form-data; name = "t"
- 1
- -----------------------------7db2611a404a4
- Content-Disposition: form-data; name = "g"
- 提交
- -----------------------------7db2611a404a4--
- -----------------------------7db2611a404a4
- 是字段间隔符。
- < pre name = "code" class = "html" style = "background-color: rgb(255, 255, 255); text-align: -webkit-left; " > -----------------------------7db2611a404a4-- </ pre >
- < pre > </ pre >
- < pre name = "code" class = "html" style = "background-color: rgb(255, 255, 255); text-align: -webkit-left; " > 是结束符。 </ pre > < pre name = "code" class = "html" style = "background-color: rgb(255, 255, 255); text-align: -webkit-left; " > < input type = "file" > 对应的值表示为: </ pre > < pre name = "code" class = "html" style = "background-color: rgb(255, 255, 255); text-align: -webkit-left; " > < pre name = "code" class = "html" style = "background-color: rgb(255, 255, 255); text-align: -webkit-left; " > -----------------------------7db2611a404a4
- Content-Disposition: form-data; name = "boy" ; filename = "C:\Users\jxq\Desktop\test.txt"
- Content-Type: text/plain
- Hello World!
- Hello World!
- Hello World!</ pre > < pre name = "code" class = "html" style = "background-color: rgb(255, 255, 255); text-align: -webkit-left; " > 即第一行是 Content-Disposition、name和客户端上传的文件的目录,第二行是上传的文件类型,第三行是空行,接下来是文件内容。 </ pre > < pre name = "code" class = "html" style = "background-color: rgb(255, 255, 255); text-align: -webkit-left; " > 而表单提交的其他属性值则是通过 Content-Disposition: from-dat; name = xx 来表示。 </ pre > < pre name = "code" class = "html" style = "background-color: rgb(255, 255, 255); text-align: -webkit-left; " > 所以为了获取上传的文件的真正内容,我们不能简单地读取从request获得的输入流,必须进一步解析。 </ pre >
- < pre > </ pre >
- < pre name = "code" class = "html" style = "background-color: rgb(255, 255, 255); text-align: -webkit-left; " > </ pre > </ pre >
- < pre > </ pre >
- < pre > </ pre >
参考链接:
http://blog.csdn.net/kanaka10/article/details/6526630
http://zhangjunhd.blog.51cto.com/113473/19631
相关推荐
将数据导出到Excel源代码及方法:response.setContentType("application/vnd.ms-excel");//响应正文的MIME类型,表示Excel response.addHeader("Content-Disposition", "attachment;filename=logininfo.xls"); ...
// 3:设置允许上传文件的大小 .这里是3m su.setMaxFileSize(3 * 1024 * 1024); // 4:初始化,接受页面传递过来的请求 su.initialize(getServletConfig(), request, response); // 5:上传 su....
commons-fileupload-1.2.1实现文件上传 需导入commons-fileupload-1.2.1.jar和 commons-io-1.3.2.jar upload2.jsp <%@ page language="java" import="java.util.*" pageEncoding="gb2312"%> <!DOCTYPE ...
// 设置上传文件时用于临时存放文件的内存大小,这里是4K.多于的部分将临时存在硬盘 dfif.setRepository(new File(request.getRealPath("/") + "ImagesUploadTemp"));// 设置存放临时文件的目录,web根目录下的...
部分代码如下,下载看全部代码: <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 登陆</title> </head> <center><br> <br> <p><form action="<%=request.getContextPath ()%>/...
response.setContentType("image/jpeg"); // ����ҳ�治���� response.setHeader("Pragma", "No-cache"); response.setHeader("Cache-Control", "no-cache"); response.setDateHeader("Expires", 2000)...
简单的利用java与js实现文件上传 package com.fendou.myString; import java.io.File; import java.io.IOException; import java.io.PrintWriter; import java.util.Iterator; import javax.servlet....
* @param response the response send by the server to the client * @throws ServletException if an error occurred * @throws IOException if an error occurred */ public void doGet...
部分代码如下,下载可看查看全部: 1.数据库结构(为简便这边采用access,实际应用中建议采用其他数据库如MySQL,MSSQL等) ============================== uname 用户名 文本型 pword 密码 文本型 初始数据uname ...
package org.news.servlet.back; import java.io.IOException; import java.io.PrintWriter; import java.sql.SQLException; import java.util.List; import javax.servlet.ServletContext; import javax.servlet....
package AiSoft.OwnHome.Servlet; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet....
* This class demonstrates how JDBC can be used within a servlet. It uses * initialization parameters (which come from the web.xml configuration file) * to create a single JDBC database connection, ...
* @param response the response send by the server to the client * @throws ServletException if an error occurred * @throws IOException if an error occurred */ public void doGet...
由于response是jsp页面中的隐含对象,故在jsp页面中可以用response.sendRedirect()直接实现重定位。 注意: (1) 使用response.sendRedirect时,前面不能有HTML输出 这并不是绝对的,不能有HTML输出其实是...
// response.sendRedirect("/sshmvc/listUser.jsp"); request.getRequestDispatcher("/listUser.jsp") .forward(request, response); } } import="java.util.*,java.util.*,com.sshmvc.*,...
charset=gb2312″%> 如果是Servlet文件,查看: 代码如下:response.setContentType(“text/html;charset=gb2312”); 您可能感兴趣的文章:jsp页面中获取servlet请求中的参数的办法详解JavaWeb实现用户登录注册功能...
* @param response the response send by the server to the client * @throws ServletException if an error occurred * @throws IOException if an error occurred */ public void doPost...
jspsmartupload.jar组件大家都知道,但是它本身自带的download功能并不支持中文名称的文件,在下载的时候会出现乱码,我自己编写了一个FileDownload类,放到了这个jar包中,这个类用的UTF-8编码方式,所以可以对中文...
response.sendRedirect("choosesuc.jsp");//选课成功页面 } /** * Initialization of the servlet. * * @throws ServletException if an error occurs */ public void init() throws ServletException...
PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd"> <display-name>My Web Application</display-name> A application for test. </description> </...