博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Servlet中文件上传
阅读量:6084 次
发布时间:2019-06-20

本文共 3253 字,大约阅读时间需要 10 分钟。

利用getReader()和getInputstream()上传

package control;import javax.servlet.ServletException;import javax.servlet.ServletInputStream;import javax.servlet.annotation.WebServlet;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import java.io.*;@WebServlet("/fileupload")public class Fileupload extends HttpServlet {    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {      //request.setCharacterEncoding("utf-8");      //方法一      /*  ServletInputStream sis=request.getInputStream();        byte[] tmp=new byte[8192];        File file=new File("E:\\dc.zip");        FileOutputStream fos=new FileOutputStream(file);        int i=sis.read(tmp);        while (i!=-1){            fos.write(tmp,0,i);            i=sis.read(tmp);        }        fos.close();*/      //方法二        BufferedReader br=request.getReader();        /*File file=new File("E:\\da.doc");        BufferedWriter bw=new BufferedWriter(new FileWriter(file));*/        String s=br.readLine();        while (s!=null){            System.out.println(s);            s=br.readLine();        }        br.close();        PrintWriter out=response.getWriter();        out.write("success");        out.close();    }    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {    }}

Servlet3.0中Part,getPart()文件上传

package control;import javax.servlet.ServletException;import javax.servlet.annotation.MultipartConfig;import javax.servlet.annotation.WebServlet;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import javax.servlet.http.Part;import java.io.IOException;@MultipartConfig(location = "e:\\",maxFileSize = 1024*1024*2)//保存文件路径@WebServlet("/ServletFile")public class ServletFile extends HttpServlet {    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {        request.setCharacterEncoding("utf-8");        Part part=request.getPart("file");        String head=part.getHeader("Content-Disposition");//获取文件头Content-Disposition        System.out.println(head);        //获取文件名        int start=head.lastIndexOf("=\"");        String filename=head.substring(start+2);        filename=filename.substring(0,filename.length()-1);        System.out.println(filename);        part.write(filename);    }    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {    }}

 多文件上传getParts()就是把单文件用foreach()处理

//多文档上传        request.setCharacterEncoding("utf-8");        response.setContentType("text/html;charset=utf-8");        for (Part part:request.getParts()){            String head=part.getHeader("Content-Disposition");//获取文件头Content-Disposition            System.out.println(head);            //获取文件名            int start=head.lastIndexOf("=\"");            String filename=head.substring(start+2);            filename=filename.substring(0,filename.length()-1);            System.out.println(filename);            part.write(filename);

 

转载于:https://www.cnblogs.com/feipengting/p/7610134.html

你可能感兴趣的文章
nginx搭建mp4和flv播放器
查看>>
CGI的函数
查看>>
异常与日志
查看>>
神经网络实现连续型变量的回归预测(python)
查看>>
python中关于正则表达式三
查看>>
努力吧.关于工厂模式(一)
查看>>
Python逻辑判断顺序
查看>>
curl http_code
查看>>
sql反模式分析1
查看>>
IPython,让Python显得友好十倍的外套——windows XP/Win7安装详解
查看>>
SQL连接查询
查看>>
数据库中一些简单的防刷机制
查看>>
CSS中文字体对照表
查看>>
Cartographer源码阅读(3):程序逻辑结构
查看>>
简述 OAuth 2.0 的运作流程
查看>>
Hive通过查询语句向表中插入数据注意事项
查看>>
给ListBox每项加图标
查看>>
间接赋值0级指针到一级指针
查看>>
5.限制结果集范围
查看>>
二叉查找树
查看>>