纯servlet文件上传

对于学习JSP/SERVLET的人来说,学会使用jar包实现文件上传的只是一种入门,就好像别人写了一个类,你知道调用类的main方法一样。但是,香港虚拟主机,我们不仅要知道怎么调用main方法,更要知道main方法中写的是什么。最近对文件上传下载感兴趣,就找了找相关资料,香港空间,结合自己所学,做了一个文件上传的例子。便于大家的学习。

   例子的功能:

文件只有两个。

1.test.jsp

pageEncoding首页enctype值1:值2:路径1:路径2:

2.FileUpload.java

import java.io.BufferedOutputStream;/** * 上传附件 */@WebServlet(“/FileUpload”)public class FileUpload extends HttpServlet {private String fpath=”c:/”;private Map<String, String> map=new HashMap<String, String>();private List<File> files=new ArrayList<File>();private long getFileUpload(HttpServletRequest request) throws IOException{long start=System.currentTimeMillis();ServletInputStream in = request.getInputStream();getFileUpload(in,getSign(request),fpath);long end=System.currentTimeMillis();return end-start;}public String getSign(HttpServletRequest request){String contentType = request.getContentType();int index = contentType.indexOf( “boundary=” );return “–” + contentType.substring( index + 9 );}private void getFileUpload(ServletInputStream in,String sign, String fpath) throws IOException {int l=0;byte[] b=new byte[1024];//用来指示要上传的文件File file=null;;BufferedOutputStream out=null;//用来存放form表单中的value的值。这儿考虑到textarea等,所以用到了sbStringBuilder sb=new StringBuilder();//用来存放form中的name值String;while((l=in.readLine(b, 0, b.length))>0){if (file==null) {String temp=new String(b,0,l);String lowerTemp=temp.toLowerCase();if (lowerTemp.startsWith( “content-disposition: form-data; ” )) {if (lowerTemp.contains(“filename=\””) && !lowerTemp.contains(“filename=\”\””)) {int nIndex=lowerTemp.indexOf(“filename=\””);int nLastIndex=temp.indexOf(“\””,nIndex+10);String filePath=temp.substring(nIndex+10, nLastIndex);String filename=filePath.substring(filePath.lastIndexOf(“\\”)+1);file=new File(fpath+filename);out=new BufferedOutputStream(new FileOutputStream(file));in.readLine(b, 0, b.length);}else if (lowerTemp.contains(“name=\””)) {name=temp.substring(lowerTemp.indexOf(“name=\””)+6,temp.lastIndexOf(“\””));}in.readLine(b, 0, b.length);}else if (!””.equals(name)){if (temp.startsWith(sign)) {map.put(name, sb.toString());name=””;sb=new StringBuilder();}elsesb.append(new String(b,0,l-2));}continue;}if (l==sign.length()+2 || l==sign.length()+4) {String temp=new String(b,0,l);if (temp.startsWith(sign)) {out.flush();out.close();files.add(new File(file.getAbsolutePath()));file=null;out=null;continue;}}out.write(b,0,l);out.flush();}if (file!=null) {files.add(new File(file.getAbsolutePath()));}}protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {request.setCharacterEncoding(“GBK”);long time=getFileUpload(request);System.out.println(“上传文件花费时间为:”+time+”毫秒,文件上传位置为:”+fpath);System.out.println(“form表单的值为:”);for (String key : map.keySet()) {System.out.println(key+”:”+map.get(key));}System.out.println();System.out.println(“上传的文件有:”);for(File file:files){System.out.println(file.getName());}response.getWriter().write(“success!!!”);}}

3.附送一个ServletInputStream读取的内容:

即使爬到最高的山上,一次也只能脚踏实地地迈一步。

纯servlet文件上传

相关文章:

你感兴趣的文章:

标签云: