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: ...
随机推荐
- ElasticSearch学习笔记(四)-- 分布式
1. 分布式介绍及cerebro cerebro插件 点击release下载 解压运行 访问9000端口,连接es的9200端口 2. 构建集群 新增一个节点 3. 副本与分片 再加入一个节点 4. ...
- 《Cracking the Coding Interview》——第3章:栈和队列——题目4
2014-03-18 05:28 题目:你肯定听过汉诺威塔的故事:三个柱子和N个从小到大的盘子.既然每次你只能移动放在顶上的盘子,这不就是栈操作吗?所以,请用三个栈来模拟N级汉诺威塔的玩法.放心,N不 ...
- 【Regularization】林轩田机器学习基石
正则化的提出,是因为要解决overfitting的问题. 以Linear Regression为例:低次多项式拟合的效果可能会好于高次多项式拟合的效果. 这里回顾上上节nonlinear transf ...
- Python 3基础教程11-如何利用pip命令安装包和模块
本文介绍如何利用pip命令安装Python相关的包和模块.在Python中有些方法或者模块是自带的功能,也叫(build-in),内构函数,实际使用,可能内构函数或者模块不能完成我们的任务,我们就需要 ...
- php 根据文件内容来判断文件类型
/*文件扩展名说明 *7173 gif *255216 jpg *13780 png *6677 bmp *239187 txt,aspx,asp,sql *208207 xls.doc.ppt *6 ...
- C++寒假学习计划
课程 中国大学mooc西北工业大学c++程序设计 理由 本课程有48节,章节分类清晰,由许多小知识块组成,条例清晰便于学习,由基础开始,由浅入深,适合我这种小白. 计划 从2.8号至2.28除去2.1 ...
- FOJ Problem 1016 无归之室
Problem 1016 无归之室 Accept: 926 Submit: 7502Time Limit: 1000 mSec Memory Limit : 32768 KB Prob ...
- C#利用VFW实现摄像头程序
最近在搞这个考试监控,找来VFW的资料,胡编乱凑而成. VFW全称为Video for Windows,是微软提供的,内嵌windows系统. 首先定义一个VideoAPI类. 首先调用avicap3 ...
- hadoop2.5.2学习及实践笔记(六)—— Hadoop文件系统及其java接口
文件系统概述 org.apache.hadoop.fs.FileSystem是hadoop的抽象文件系统,为不同的数据访问提供了统一的接口,并提供了大量具体文件系统的实现,满足hadoop上各种数据访 ...
- [hdu6434]Problem I. Count
题目大意:$T(T\leqslant 10^5)$组数据,每组数据给你$n(n\leqslant 2\times 10^7)$,求$\sum\limits_{i=1}^n\sum\limits_{j= ...