JAVA SERVLET上传文件的样码
import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.io.PrintWriter; import javax.servlet.ServletContext; 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; /** * Servlet implementation class FileUploadServlet */ @WebServlet(name = "FileUploadServlet", urlPatterns = {"/upload"}) @MultipartConfig public class FileUploadServlet extends HttpServlet { private static final long serialVersionUID = 1L; protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=utf-8"); request.setCharacterEncoding("utf-8"); ServletContext servletContext = this.getServletContext(); String realPath = servletContext.getRealPath("/upload"); final Part filePart = request.getPart("file"); final String fileName = getFileName(filePart); OutputStream out = null; InputStream filecontent = null; final PrintWriter writer = response.getWriter(); try { System.out.println(realPath + "@@@@@@@@@@@@@@@@@@@@@@@@@"); System.out.println( File.separator + "@@@@@@@@@@@@@@@@@@@@@@@@@"); System.out.println( fileName + "@@@@@@@@@@@@@@@@@@@@@@@@@"); out = new FileOutputStream( new File(realPath + File.separator + fileName)); filecontent = filePart.getInputStream(); int read; final byte[] bytes = new byte[1024]; while ((read=filecontent.read(bytes)) != -1) { out.write(bytes, 0, read); } writer.println("upload file " + fileName + " to path " + realPath); } catch (FileNotFoundException fne) { writer.println("no upload path or upload path is wrong."); writer.println("<br/> error: " + fne.getMessage()); } finally { if (out !=null) { out.close(); } if (filecontent != null){ filecontent.close(); } if (writer != null) { writer.close(); } } } private String getFileName(final Part part) { // TODO Auto-generated method stub for (String content: part.getHeader("content-disposition").split(";")) { System.out.println(content + "####################"); if (content.trim().startsWith("filename")) { String filename = content.substring( content.lastIndexOf("\\") + 1).trim().replace("\"", ""); System.out.println(filename + "####################"); return "2.zip"; } } return null; } /** * @see HttpServlet#HttpServlet() */ public FileUploadServlet() { super(); // TODO Auto-generated constructor stub } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub processRequest(request, response); } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub processRequest(request, response); } }
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Login</title> </head> <body> <form method="POST" action="upload" enctype="multipart/form-data"> choose a file: <input type="file" name=file id="file" /><br/><br/> <input type="submit" value="upload" name="upload" id="upload" /> </form> </body> </html>
JAVA SERVLET上传文件的样码的更多相关文章
- java servlet上传文件并把文件内容显示在网页中
servlet3.0(JDK1.6)自带的API即可实现本地文件的上传,Servlet3.0新增了Part接口,HttpServletRequest的getPart()方法取得Part实现对象.下面我 ...
- JAVA servlet 上传文件(commons-fileupload, commons-io)
<1>获取二进制文件流并输出 InputStream inputStream = request.getInputStream(); BufferedReader reader = new ...
- Servlet上传文件
Servlet上传文件 1.准备工作 (1)利用FileUpload组件上传文件,须要到apache上下载commons-fileupload-1.3.1.jar 下载地址:http://common ...
- 原生Servlet 上传文件
依赖jar <dependency> <groupId>commons-fileupload</groupId> <artifactId>commons ...
- java 后台上传文件
java 后台上传文件 public static String uploadFile(File file, String RequestURL) throws IOException { Strin ...
- 使用Servlet上传文件
使用浏览器向服务器上传文件其本质是打开了一个长连接并通过TCP方式传输数据.而需要的动作是客户端在表单中使用file域,并指定该file域的name值,然后在form中设定enctype的值为mult ...
- Java ftp上传文件方法效率对比
Java ftp上传文件方法效率对比 一.功能简介: txt文件采用ftp方式从windows传输到Linux系统: 二.ftp实现方法 (1)方法一:采用二进制流传输,设置缓冲区,速度快,50M的t ...
- Java+Selenium 上传文件,点击选择“浏览文件”按钮,报错invalid argument
Java+Selenium 上传文件,点击选择"浏览文件"按钮,报错invalid argument 解决代码: Actions action=new Actions(driver ...
- servlet上传文件报错(三)
1.具体报错如下 null null Exception in thread "http-apr-8686-exec-5" java.lang.OutOfMemoryError: ...
随机推荐
- python和matlab
一.python简介 python是一种面向对象的解释型计算机程序设计语言.python是纯粹的自由软件,源代码和解释器CPython遵循GPL协议.Python语法简介清晰,特色之一是强制用空白符作 ...
- <<程序猿健康指南>> 笔记
序言: 长时间对着电脑工 作.一天下来基本不怎么走动的人,患高血压及 2 型糖尿病的风险远高于其他人群, 这两种疾病会对人体的健康产生长久的严重影响,还会增加心脏病及中风的几率. 前言: 阿米什人(A ...
- nodejs 操作mongodb, 增删改查
很久没有学node了,之前书看了一半,今天继续学发现版本问题很坑爹,按书例子执行一堆错误.想学nodejs操作db,百度半天,一堆sb写神马鸟玩儿?简简单单写一大堆还运行不了的.需要代码也是看别人写的 ...
- linux统计分析流量-wireshark
wireshark是一款带界面的开源抓包工具,可以用来对系统流量进行统计分析. 安装 由于wireshark是带界面的,所以一般在界面环境下运行,可以通过yum安装: $ yum install -y ...
- Python第三方模块tesserocr安装
介绍 在爬虫过程中,难免会遇到各种各样的验证码,而大多数验证码还是图形验证码,这时候我们可以直接用 OCR 来识别. tesserocr 是 Python 的一个 OCR 识别库 ,但其实是对 tes ...
- 社区版pycharm安装Django框架
1.cmd下执行:pip3 install django 2.cmd下执行:django-admin startproject Demo (Demo为项目名称,可以更改你取的项目名称) 3.cmd下执 ...
- 解决display:inline-block;行内块元素出现空白空隙问题
给他的父元素加:font-size:0px;, ul { font-size:0px;} li { font-size:16px;}
- jenkins shell脚本构建项目--留
1.echo `date` "doing compling . . . " if [ "$RELEASE" == "false" ]; ...
- 牛客网暑期ACM多校训练营(第一场):J-Different Integers(分开区间不同数+树状数组)
链接:J-Different Integers 题意:给出序列a1, a2, ..., an和区间(l1, r1), (l2, r2), ..., (lq, rq),对每个区间求集合{a1, a2, ...
- 【视觉SLAM14讲】ch4心得与课后题答案【仅供参考】
答案: Q1:验证SO(3) SE(3) Sim(3)关于乘法成群 SO(3) : 由P64最开始可知,乘法代表了旋转,而SO(3)是旋转矩阵的集合, SE(3) Sim(3) 同理(最基础的部分 ...