dwr上传文件
2016-01-14 16:42
246 查看
1.Java中的代码
public String upload(InputStream is,String filename) throws IOException {
//WebContext可以获取HttpServlet的对象
WebContext wc = WebContextFactory.get();
HttpServletRequest req = wc.getHttpServletRequest();
String realpath = req.getSession().getServletContext().getRealPath("upload");
String fn = FilenameUtils.getName(filename);
String filepath = realpath+"/"+fn;
FileUtils.copyInputStreamToFile(is, new File(filepath));
return filepath;
}
2.jsp中的代码
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<script type="text/javascript" src="<%=request.getContextPath()%>/dwr/engine.js"></script>
<script type="text/javascript" src="<%=request.getContextPath()%>/dwr/util.js"></script>
<script type="text/javascript" src="<%=request.getContextPath()%>/dwr/interface/MyDwr.js"></script>
<script type="text/javascript">
function upload() {
var file = dwr.util.getValue("myfile");
alert(file.value);
alert(file);
MyDwr.upload(file,file.value,function(data){
alert(data);
});
}
</script>
</head>
<body>
<input type="file" id="myfile"/>
<input type="button" value="上传文件" onclick="upload()"/>
</body>
</html>
3.如果要限制上传文件的大小,需要在web.xml中配置
<init-param>
<param-name>fileUploadMaxBytes</param-name>
<param-value>25000</param-value>
</init-param>
public String upload(InputStream is,String filename) throws IOException {
//WebContext可以获取HttpServlet的对象
WebContext wc = WebContextFactory.get();
HttpServletRequest req = wc.getHttpServletRequest();
String realpath = req.getSession().getServletContext().getRealPath("upload");
String fn = FilenameUtils.getName(filename);
String filepath = realpath+"/"+fn;
FileUtils.copyInputStreamToFile(is, new File(filepath));
return filepath;
}
2.jsp中的代码
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<script type="text/javascript" src="<%=request.getContextPath()%>/dwr/engine.js"></script>
<script type="text/javascript" src="<%=request.getContextPath()%>/dwr/util.js"></script>
<script type="text/javascript" src="<%=request.getContextPath()%>/dwr/interface/MyDwr.js"></script>
<script type="text/javascript">
function upload() {
var file = dwr.util.getValue("myfile");
alert(file.value);
alert(file);
MyDwr.upload(file,file.value,function(data){
alert(data);
});
}
</script>
</head>
<body>
<input type="file" id="myfile"/>
<input type="button" value="上传文件" onclick="upload()"/>
</body>
</html>
3.如果要限制上传文件的大小,需要在web.xml中配置
<init-param>
<param-name>fileUploadMaxBytes</param-name>
<param-value>25000</param-value>
</init-param>
相关文章推荐
- 16-01-14 TextView 属性
- leveldb代码阅读(5)——恢复数据库的状态
- 另外几种Java集合框架详解
- windows 环境下面nginx的安装与配置
- redis实现分布式锁
- [未完成]游戏粒子系统的优化相关
- linux 文件查找
- 个人总结
- 把电脑·系统属性·注册到(JS的名字,电话)给更改掉
- 之字打印树
- SVN服务器端安装、配置与管理
- Java调用 新浪微博API 接口发微博,逐项讲解,绝对清晰
- mysql 源码安装
- 自定义瀑布流
- bzoj3514: Codechef MARCH14 GERALD07加强版
- Jquery实现列表项的快速检索
- [IMX6Q][Android5.1]移植笔记 --- uboot_v2015.04移植
- 电气人员操作注意
- Linux 下zip包的压缩与解压
- pod install 失效问题