Servlet实现文件上传(简单)(一)


<%@ 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">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<form action="UploadServlet" method="post" enctype="multipart/form-data">
username: <input type="text" name="username"><br>
file: <input type="file" name="file"><br>
file2: <input type="file" name="file2"><br>
<input type="submit" value="submit">
</form>
3、servlet处理UploadServlet.java
package com.shengsiyuan.servlet;
import java.io.File;
import java.io.IOException;
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.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
public class UploadServlet extends HttpServlet
{
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException
{
DiskFileItemFactory factory = new DiskFileItemFactory();
String path = req.getRealPath("/upload");//定义上传文件的路径为webroot下的upload文件
factory.setRepository(new File(path));
factory.setSizeThreshold(1024 * 1024);//上传文件的大小为1024*1024字节,也就是1兆
ServletFileUpload upload = new ServletFileUpload(factory);
try
{
List<FileItem> list = (List<FileItem>)upload.parseRequest(req);
for(FileItem item : list)
{
String name = item.getFieldName();
if(item.isFormField())//如果上传的是文本域的话,对文本与进行处理
{
String value = item.getString();
System.out.println(name + "=" + value);
req.setAttribute(name, value);
}
else//如果上传的是文件
{
String value = item.getName();
int start = value.lastIndexOf("\\");//查找文件名称,从最后一个\开始查找
String fileName = value.substring(start + 1);//这时候对应的文件应该从1开始,因为0是\
req.setAttribute(name, fileName);
item.write(new File(path, fileName));//将文件写到磁盘上来,path为上传的路径,filename为文件名称
//以下是自己写的操作输入输出流的代码,当然也可以不使用下面的写法
//
// OutputStream os = new FileOutputStream(new File(path, fileName));
//
// InputStream is = item.getInputStream();
//
// byte[] buffer = new byte[400];
//
// int length = 0;
//
// while((length = is.read(buffer)) != -1)
// {
// os.write(buffer, 0, length);
// }
//
// is.close();
// os.close();
}
}
}
catch(Exception ex)
{
ex.printStackTrace();
}
req.getRequestDispatcher("fileUploadResult.jsp").forward(req, resp);
}
}
4、结果页fileUploadResult.jsp
<%@ page language="java" import="java.util.*, java.io.*" 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 'fileUploadResult.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>
<%
/*
InputStream is = request.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(is));
String buffer = null;
while(null != (buffer = br.readLine()))
{
out.print(buffer + "<br>");
}
br.close();
is.close();
*/
%>
username : ${requestScope.username }<br>
file: ${requestScope.file}<br>
file2: ${requestScope.file2 }<br>
</body>
</html>
5、页面展示
结果展示
Servlet实现文件上传(简单)(一)的更多相关文章
- Web---演示Servlet的相关类、表单多参数接收、文件上传简单入门
说明: Servlet的其他相关类: ServletConfig – 代表Servlet的初始化配置参数. ServletContext – 代表整个Web项目. ServletRequest – 代 ...
- Servlet实现文件上传
一.Servlet实现文件上传,需要添加第三方提供的jar包 下载地址: 1) commons-fileupload-1.2.2-bin.zip : 点击打开链接 2) commons- ...
- Servlet实现文件上传,可多文件上传
一.Servlet实现文件上传,需要添加第三方提供的jar包 接着把这两个jar包放到 lib文件夹下: 二: 文件上传的表单提交方式必须是POST方式, 编码类型:enctype="mul ...
- 配置servlet支持文件上传
Servlet3.0为Servlet添加了multipart配置选项,并为HttpServletRequest添加了getPart和getParts方法获取上传文件.为了使Servlet支付文件上传需 ...
- jsp+servlet实现文件上传下载
相关素材下载 01.jsp <%@ page language="java" contentType="text/html; charset=UTF-8" ...
- java commons-fileupload servlet 多文件上传
commons-fileupload servlet 多文件上传 需要引入的 jar 包. commons-fileupload-1.3.2.jar commons-io-2.2.jar 工程路劲:查 ...
- 使用FileUpload实现Servlet的文件上传
简介 FileUpload 是 Apache commons下面的一个子项目,用来实现Java环境下的文件上传功能. FileUpload链接 FileUpload 是基于Apache的Commons ...
- Servlet实现文件上传和下载
对于文件上传,浏览器在上传的过程中是将文件以流的形式提交到服务器端的,如果直接使用Servlet获取上传文件的输入流然后再解析里面的请求参数是比较麻烦,所以一般选择采用apache的开源工具commo ...
- Spring MVC 文件上传简单示例(form、ajax方式 )
1.Form Upload SpringMVC 中,文件的上传是通过 MultipartResolver 实现的,所以要实现上传,只要注册相应的 MultipartResolver 即可. Multi ...
- servlet web文件上传
web文件上传也是一种POST方式,特别之处在于,需设置FORM的enctype属性为multipart/form-data. 并且需要使用文件域. servlet的代码比较关键是这几句: // 使用 ...
随机推荐
- 修改config.php配置
$data=array( "name"=>"222222", "tel"=>159131, "address" ...
- 第一个前台页面----xflow的页面
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%><%@ tagl ...
- BinTools 十六进制转换
package de.rtner.misc; public class BinTools { public static final String hex = "0123456789ABCD ...
- hdu Eddy's picture (最小生成树)
Eddy's picture Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other) Tota ...
- 转:Jmeter进行分布式性能测试
由于Jmeter本身的瓶颈,当需要模拟数以千计的并发用户时,使用单台机器模拟所有的并发用户就有些力不从心,甚至还会引起JAVA内存溢出的错误.要解决这个问题,可以使用分布式测试,运行多台机器运行所谓的 ...
- 转:Loadrunner——Simulate a new user on each iteration设置
最近在与大家的讨论中发现了LoadRunner的很多问题,出于解决问题的出发点,我也就相关自己不理解的问题在Google中搜索了一番,并通过一些实例也去实际操作了一遍,发现很多问题确实并不是那么难解决 ...
- HDU 1814 Peaceful Commission
2-SAT,输出字典序最小的解,白书模板. //TwoSAT输出字典序最小的解的模板 //注意:0,1是一组,1,2是一组..... #include<cstdio> #include&l ...
- FTP 7.5 自定义扩展功能
@import url(http://i.cnblogs.com/Load.ashx?type=style&file=SyntaxHighlighter.css);@import url(/c ...
- hdu_5718_Oracle(大数模拟)
题目连接:hdu_5718_Oracle 题意: 给你一串数,让你分出两个正整数,使其和最大,若不能分出来就输出"Uncertain" 题解: 当时比赛的时候还天真的去搞大数模版, ...
- android脚步--Relativelayout设置
引自http://blog.csdn.net/lamp_zy/article/details/8035161 http://my.oschina.net/honeyming/blog/130761 以 ...