1.fileupload.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'fileupload.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
</head>
<body>
<!-- enctype 默认是 application/x-www-form-urlencoded -->
<form action="FileUpLoad" enctype="multipart/form-data" method="post" >
用户名:<input type="text" name="usename"> <br/>
上传文件:<input type="file" name="file1"><br/>
上传文件: <input type="file" name="file2"><br/>
<input type="submit" value="提交"/>
</form>
</body>
</html>
2.实际处理文件上传的 FileUpLoad.java
package com.servlet.fileupload;
import java.io.File;
import java.io.*;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
public class FileUpLoad extends HttpServlet {
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("utf-8"); //设置编码
//获得磁盘文件条目工厂
DiskFileItemFactory factory = new DiskFileItemFactory();
//获取文件需要上传到的路径
String path = this.getServletContext().getRealPath("/upload");
factory.setRepository(new File(path));
//设置 缓存的大小,当上传文件的容量超过该缓存时,直接放到 暂时存储室
factory.setSizeThreshold(1024*1024) ;
//高水平的API文件上传处理
ServletFileUpload upload = new ServletFileUpload(factory);
try {
//可以上传多个文件
List<FileItem> list =upload.parseRequest(request);
for(FileItem item : list)
{
//获取表单的属性名字
String name = item.getFieldName();
//如果获取的 表单信息是普通的 文本 信息
if(item.isFormField())
{
//获取用户具体输入的字符串 ,名字起得挺好,因为表单提交过来的是 字符串类型的
String value = item.getString() ;
request.setAttribute(name, value);
}
//对传入的非 简单的字符串进行处理 ,比如说二进制的 图片,电影这些
else
{
/**
* 以下三步,主要获取 上传文件的名字
*/
//获取路径名
String value = item.getName() ;
//索引到最后一个反斜杠
int start = value.lastIndexOf("\\");
//截取 上传文件的 字符串名字,加1是 去掉反斜杠,
String filename = value.substring(start+1);
request.setAttribute(name, filename);
//真正写到磁盘上
OutputStream out = new FileOutputStream(new File(path,filename));
InputStream in = item.getInputStream() ;
int length = 0 ;
byte [] buf = new byte[1024] ;
System.out.println("获取上传文件的总共的容量:"+item.getSize());
// in.read(buf) 每次读到的数据存放在 buf 数组中
while( (length = in.read(buf) ) != -1)
{
//在 buf 数组中 取出数据 写到 (输出流)磁盘上
out.write(buf, 0, length);
}
in.close();
out.close();
}
}
} catch (FileUploadException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (Exception e) {
e.printStackTrace();
}
request.getRequestDispatcher("filedemo.jsp").forward(request, response);
}
}
3.filedemo.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'filedemo.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
用户名:${requestScope.usename } <br/>
文件:${requestScope.file1 }<br/>
${requestScope.file2 }<br/>
<!-- 把上传的图片显示出来 -->
<img alt="go" src="upload/<%=(String)request.getAttribute("file1")%> " />
</body>
</html>
相关推荐
主要为大家详细介绍了Servlet实现文件上传功能,还可以实现Servlet多文件上传功能,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
Java实现的非第三方jar包的文件上传功能,本人亲测通过 通过解析HTTP协议以及文件流实现
本文实例为大家分享了JSP+Servlet实现文件上传到服务器功能的具体代码,供大家参考,具体内容如下 项目目录结构大致如下: 正如我在上图红线画的三个东西:Dao、service、servlet 这三层是主要的结构,类似 MVC ...
Servlet实现文件上传与下载 Servlet 文件上传与下载
开发工具是myEclipse,用 Servlet实现文件上传,通过commons-fileupload-1.2.1.jar commons-io-1.4.jar插件实现了文件上传
一、Servlet实现文件上传,需要添加第三方提供的jar包 下载地址: 1) commons-fileupload-1.2.2-bin.zip: 点击打开链接 2) commons-io-2.3-bin.zip: 点击打开链接 接着把这两个jar包放到 lib文件夹下: 二:...
servlet jsp javabean 实现的文件上传
使用CKEditor3.6.0版本,使用上传组件实现Servlet上传图片,含有列子
使用Servlet实现文件的上传与下载Demo
使用servlet实现简易文件上传,带有进度条
servlet实现的上传文件实例
vue,servlet3.0、文件上传
【摘要】:该文介绍了在B/S模式下,通过JSP/Servlet技术实现文件上传的方法。 文件上传在很多网站应用中是必不可少的,在电子商务中上传商品图片,在音乐网站中上传流行歌曲……一个高效率的文件上传功能尤为重要。...
今天和大家分享下基于servlet的方式实现文件上传; 1、众所周知在不实用框架的时候使用表单模式实现文件上传是无法接收其他的参数的; 2、但是在使用mutipart/form-data模式上传的时候,其实非文件属性的属性的值是...
Jcreator pro开发工具。需要导入的common和servlet-api.jar包...上传文件页面,选择文件。提交后转到 servlet服务器。 /FileUpload 处理上传文件。显示文件名称,和上传后路径。things文件夹作为存储上传文件文件夹。
该资源利用开源upload文件包,成功实现文件上传与下载
com.fm.FileManagerService:一个servlet用来实现主要的文件上传下载逻辑的 com.fm.MyPreogressListener:一个进度监听类,用来做上传进度条的 jquery-1.9.1.js index.jsp:文件列表页面 upload.jsp:文件上传form...
Ajax + Servlet 实现上传文件进度条显示
利用Ajax和Servlet实现文件上传,用来commons-fileupload和commons-io两个包。这两个包也在里边。东西做的很简单,但重点是能对这个方法有所了解就行了,有人想做的更炫的可以自行修改。希望能对大家有所帮助。如果...