http://blog.csdn.net/feichexia/article/details/7105741#
来源:互联网 发布:威客网络兼职打字员 编辑:程序博客网 时间:2024/05/21 20:27
- 常见的MIME类型如下表:
常见的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字符串
此外不同浏览器下对同一个文件上传后获取到的类型可能不同。
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>
<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 {
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 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);
- }
- }
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>
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>
</pre><div class="dp-highlighter bg_java"><div class="bar"><div class="tools"><strong>[java]</strong> <a target=_blank class="ViewSource" title="view plain" href="http://blog.csdn.net/feichexia/article/details/7105741#">view plain</a><a target=_blank class="CopyToClipboard" title="copy" href="http://blog.csdn.net/feichexia/article/details/7105741#">copy</a><a target=_blank class="PrintSource" title="print" href="http://blog.csdn.net/feichexia/article/details/7105741#">print</a><a target=_blank class="About" title="?" href="http://blog.csdn.net/feichexia/article/details/7105741#">?</a></div></div><ol class="dp-j"><li class="alt">accept.jsp </li><li><%<span class="annotation">@page</span> contentType=<span class="string">"text/html;charset=GB2312"</span>%> </li><li class="alt"><%@ page <span class="keyword">import</span>=<span class="string">"java.io.*"</span>%> </li><li><HTML> </li><li class="alt"><BODY> </li><li><% </li><li class="alt"><span class="comment">//经测试,说明:ServletInputStream类中的readLine(byte[] b, int off, int len) </span> </li><li><span class="comment">//其中参数 byte[] b 起缓冲作用,此方法一次读取一行,但如果 byte[] b 定义的大小,比要读取的一行需占用的空间要小,则 </span> </li><li class="alt"><span class="comment">//该方法只读取 byte[] b 指定的大小;再次读取时会继续接着上次未读完的读取;返回值 :返回实际读取的字节数,当读到文档流的 </span> </li><li><span class="comment">//最后时返回-1。 </span> </li><li class="alt"><span class="keyword">try</span>{ </li><li> ServletInputStream in=request.getInputStream(); </li><li class="alt"> File f=<span class="keyword">new</span> File(<span class="string">"c:\\test"</span>,<span class="string">"a.txt"</span>); </li><li> FileOutputStream o=<span class="keyword">new</span> FileOutputStream(f); </li><li class="alt"> </li><li><span class="comment">//如果byte b[]=设置的值太短的话(假如设置为2),那么在 应用的 上传操作时会有影响,具体表现为无法解析文档路径等相关信息 </span> </li><li class="alt"><span class="keyword">byte</span> b[]=<span class="keyword">new</span> <span class="keyword">byte</span>[<span class="number">2046</span>]; </li><li> <span class="keyword">int</span> n; </li><li class="alt"> <span class="keyword">int</span> i = <span class="number">0</span>; </li><li> <span class="keyword">while</span>((n=in.readLine(b,<span class="number">0</span>,b.length))!=-<span class="number">1</span>)<span class="comment">//ServletInputStream.readLine方法是逐行读取的。当它读完整个文件,返回-1,一般情况下返回读取的字节数 </span> </li><li class="alt"> { </li><li> i++; </li><li class="alt"> System.out.println(<span class="string">"------"</span>+i); </li><li> o.write(b,<span class="number">0</span>,n); </li><li class="alt"> } </li><li> o.close(); </li><li class="alt"> in.close(); </li><li>}<span class="keyword">catch</span>(IOException e){ </li><li class="alt"> e.printStackTrace(); </li><li>} </li><li class="alt">out.print(<span class="string">"文件已经上传"</span>); </li><li>%> </li><li class="alt"><a href=<span class="string">"c:\\test\\a.txt"</span>>查看结果</a> </li><li></body> </li><li class="alt"></HTML> </li></ol></div><pre style="DISPLAY: none" class="java" name="code">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>我上传一个test.txt文件,其内容为3行 Hello World!。之后打开C:\\test\a.txt,内容如下:
- -----------------------------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--
-----------------------------7db2611a404a4Content-Disposition: form-data; name="boy"; filename="C:\Users\xijiang\Desktop\test.txt"Content-Type: text/plainHello World!Hello World!Hello World!-----------------------------7db2611a404a4Content-Disposition: form-data; name="t"1-----------------------------7db2611a404a4Content-Disposition: form-data; name="g"提交-----------------------------7db2611a404a4--可以很明显看到内容不是我们预期的只有三行Hello World!,还多了其他的表单属性值。观察这个文件的内容格式,可以大概看出,
- -----------------------------7db2611a404a4
-----------------------------7db2611a404a4
- 是字段间隔符。
是字段间隔符。
- <PRE style="BACKGROUND-COLOR: rgb(255,255,255)" class=html name="code">-----------------------------7db2611a404a4--</PRE>
- <PRE></PRE>
- <PRE style="BACKGROUND-COLOR: rgb(255,255,255)" class=html name="code"> 是结束符。</PRE><PRE style="BACKGROUND-COLOR: rgb(255,255,255)" class=html name="code"> <input type="file">对应的值表示为:</PRE><PRE style="BACKGROUND-COLOR: rgb(255,255,255)" class=html name="code"><PRE style="BACKGROUND-COLOR: rgb(255,255,255)" class=html name="code">-----------------------------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 style="BACKGROUND-COLOR: rgb(255,255,255)" class=html name="code"> 即第一行是 Content-Disposition、name和客户端上传的文件的目录,第二行是上传的文件类型,第三行是空行,接下来是文件内容。</PRE><PRE style="BACKGROUND-COLOR: rgb(255,255,255)" class=html name="code"> 而表单提交的其他属性值则是通过 Content-Disposition: from-dat; name=xx 来表示。</PRE><PRE style="BACKGROUND-COLOR: rgb(255,255,255)" class=html name="code"> 所以为了获取上传的文件的真正内容,我们不能简单地读取从request获得的输入流,必须进一步解析。</PRE>
- <PRE></PRE>
- <PRE style="BACKGROUND-COLOR: rgb(255,255,255)" class=html name="code"></PRE></PRE>
- <PRE></PRE>
- <PRE></PRE>
- <PRE></PRE>
<div class="dp-highlighter bg_html"><div class="bar"><div class="tools"><strong>[html]</strong> <a target=_blank class="ViewSource" title="view plain" href="http://blog.csdn.net/feichexia/article/details/7105741#">view plain</a><a target=_blank class="CopyToClipboard" title="copy" href="http://blog.csdn.net/feichexia/article/details/7105741#">copy</a><a target=_blank class="PrintSource" title="print" href="http://blog.csdn.net/feichexia/article/details/7105741#">print</a><a target=_blank class="About" title="?" href="http://blog.csdn.net/feichexia/article/details/7105741#">?</a></div></div><ol class="dp-xml"><li class="alt">-----------------------------7db2611a404a4-- </li></ol></div>-----------------------------7db2611a404a4--<pre>
- 是结束符。
- <input type="file">对应的值表示为:
- <PRE style="BACKGROUND-COLOR: rgb(255,255,255)" class=html name="code">-----------------------------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 style="BACKGROUND-COLOR: rgb(255,255,255)" class=html name="code"> 即第一行是 Content-Disposition、name和客户端上传的文件的目录,第二行是上传的文件类型,第三行是空行,接下来是文件内容。</PRE><PRE style="BACKGROUND-COLOR: rgb(255,255,255)" class=html name="code"> 而表单提交的其他属性值则是通过 Content-Disposition: from-dat; name=xx 来表示。</PRE><PRE style="BACKGROUND-COLOR: rgb(255,255,255)" class=html name="code"> 所以为了获取上传的文件的真正内容,我们不能简单地读取从request获得的输入流,必须进一步解析。</PRE>
- <PRE></PRE>
- <PRE style="BACKGROUND-COLOR: rgb(255,255,255)" class=html name="code"></PRE>
- -----------------------------7db2611a404a4
- Content-Disposition: form-data; name="boy"; filename="C:\Users\jxq\Desktop\test.txt"
- Content-Type: text/plain
- Hello World!
- Hello World!
- Hello World!
- 即第一行是 Content-Disposition、name和客户端上传的文件的目录,第二行是上传的文件类型,第三行是空行,接下来是文件内容。
- 而表单提交的其他属性值则是通过 Content-Disposition: from-dat; name=xx 来表示。
- 所以为了获取上传的文件的真正内容,我们不能简单地读取从request获得的输入流,必须进一步解析。
参考链接:
http://blog.csdn.net/kanaka10/article/details/6526630
http://zhangjunhd.blog.51cto.com/113473/19631
原博客:http://blog.csdn.net/feichexia/article/details/7105741#
- http://blog.csdn.net/feichexia/article/details/7105741#
- http://blog.csdn.net/IBM_hoojo/article/details/5688947
- http://blog.csdn.net/chenlaic/article/details/6143235
- http://blog.csdn.net/eaglewood2005/article/details/4335052
- http://blog.csdn.net/mchp/article/details/3995970
- http://blog.csdn.net/v_july_v/article/details/6015165
- http://blog.csdn.net/masterz/article/details/6232585
- http://blog.csdn.net/perfectpdl/article/details/6442847
- http://blog.csdn.net/eroswang/article/details/1967243
- http://blog.csdn.net/zhvsby/article/details/5986645
- http://blog.csdn.net/dz45693/article/details/6183645
- http://blog.csdn.net/david_lv/article/details/5798003
- http://blog.csdn.net/zxingchao2009/article/details/6299313
- http://blog.csdn.net/zhanxinhang/article/details/6783766
- http://blog.csdn.net/ruanruoshi/article/details/935510
- http://blog.csdn.net/yming0221/article/details/6538527
- http://blog.csdn.net/yming0221/article/details/6528490
- http://blog.csdn.net/yming0221/article/details/6704079
- Robolectric Test-Drive Your Android Code
- datatables--阿里巴巴
- Qt for winrt结合Winrt API开发
- positian relative 的理解
- Android系统的架构
- http://blog.csdn.net/feichexia/article/details/7105741#
- N(奇数)阶幻方-java实现代码
- These query results are not updateable.Include the ROWID to get updateable results.
- chrome 版本 42.0.2288.6 dev-m更新后导致插件不兼容
- 安装和使用JD-Eclipse插件
- 2014年中国移动游戏行业研究报告
- hdu 1865 && hdu 2955 (01背包)
- 多个button点击崩溃
- js中浮点数的精度问题