第一步:需要文件包,其实就是dwr 3.0中例子所需要的包,
dwr.jar
、
commons-fileupload-1.2.jar
、
commons-io-1.3.1.jar
。
第二步:编辑web.xml,添加dwr-invoke
<servlet>
<display-name>DWR Sevlet</display-name>
<servlet-name>dwr-invoker</servlet-name>
<servlet-class>org.directwebremoting.servlet.DwrServlet</servlet-class>
<init-param>
<description>是否打开调试功能</description>
<param-name>debug</param-name>
<param-value>true</param-value>
</init-param>
<init-param>
<description>日志级别有效值为: FATAL, ERROR, WARN (the default), INFO and DEBUG.</description>
<param-name>logLevel</param-name>
<param-value>DEBUG</param-value>
</init-param>
<init-param>
<description>是否激活反向Ajax</description>
<param-name>activeReverseAjaxEnabled</param-name>
<param-value>true</param-value>
</init-param>
<init-param>
<description>在WEB启动时是否创建范围为application的creator</description>
<param-name>initApplicationScopeCreatorsAtStartup</param-name>
<param-value>true</param-value>
</init-param>
<init-param>
<description>在WEB启动时是否创建范围为application的creator</description>
<param-name>preferDataUrlSchema</param-name>
<param-value>false</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dwr-invoker</servlet-name>
<url-pattern>/dwr/*</url-pattern>
</servlet-mapping>
第三步:创建上传类FileUpload.java,编辑代码,内容如下:
package learn.dwr.upload_download;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.geom.AffineTransform;
import java.awt.image.AffineTransformOp;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import org.directwebremoting.WebContext;
import org.directwebremoting.WebContextFactory;
/**
* title: 文件上传
* @author Administrator
* @时间 2009-11-22:上午11:40:22
*/
public class FileUpload {
/**
* @param uploadImage 圖片文件流
* @param uploadFile 需要用简单的文本文件,如:.txt文件,不然上传会出乱码
* @param color
* @return
*/
public BufferedImage uploadFiles(BufferedImage uploadImage,
String uploadFile, String color) {
// uploadImage = scaleToSize(uploadImage);
// uploadImage =grafitiTextOnImage(uploadImage, uploadFile, color);
return uploadImage;
}
/**
* 文件上传时使用InputStream类进行接收,在DWR官方例中是使用String类接收简单内容
*
* @param uploadFile
* @return
*/
public String uploadFile(InputStream uploadFile, String filename)
throws Exception {
WebContext webContext = WebContextFactory.get();
String realtivepath = webContext.getContextPath() + "/upload/";
String saveurl = webContext.getHttpServletRequest().getSession()
.getServletContext().getRealPath("/upload");
File file = new File(saveurl + "/" + filename);
// if (!file.exists()) {
// file.mkdirs();
// }
int available = uploadFile.available();
byte[] b = new byte[available];
FileOutputStream foutput = new FileOutputStream(file);
uploadFile.read(b);
foutput.write(b);
foutput.flush();
foutput.close();
uploadFile.close();
return realtivepath + filename;
}
private BufferedImage scaleToSize(BufferedImage uploadImage) {
AffineTransform atx = new AffineTransform();
atx
.scale(200d / uploadImage.getWidth(), 200d / uploadImage
.getHeight());
AffineTransformOp atfOp = new AffineTransformOp(atx,
AffineTransformOp.TYPE_BILINEAR);
uploadImage = atfOp.filter(uploadImage, null);
return uploadImage;
}
private BufferedImage grafitiTextOnImage(BufferedImage uploadImage,
String uploadFile, String color) {
if (uploadFile.length() < 200) {
uploadFile += uploadFile + " ";
}
Graphics2D g2d = uploadImage.createGraphics();
for (int row = 0; row < 10; row++) {
String output = "";
if (uploadFile.length() > (row + 1) * 20) {
output += uploadFile.substring(row * 20, (row + 1) * 20);
} else {
output = uploadFile.substring(row * 20);
}
g2d.setFont(new Font("SansSerif", Font.BOLD, 16));
g2d.setColor(Color.blue);
g2d.drawString(output, 5, (row + 1) * 20);
}
return uploadImage;
}
}
第四步:添加到dwr.xml
<create creator="new">
<param name="class" value="learn.dwr.upload_download.FileUpload" />
</create>
第五步:添加前台html代码
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>二进制文件处理,文件上传</title>
<script type='text/javascript' src='/learnajax/dwr/interface/FileUpload.js'></script>
<script type='text/javascript' src='/learnajax/dwr/engine.js'></script>
<script type='text/javascript' src='/learnajax/dwr/util.js'></script>
<script type='text/javascript' >
function uploadFiles(){
var uploadImage = dwr.util.getValue("uploadImage");
FileUpload.uploadFiles(uploadImage, "", "", function(imageURL) {
alert(imageURL);
dwr.util.setValue('image', imageURL);
});
}
function uploadFile(){
var uploadFile = dwr.util.getValue("uploadFile");
//var uploadFile =document.getElementById("uploadFile").value;
var uploadFile_temp = uploadFile.value.replace("\\","/");
var filenames = uploadFile.value.split("/");
var filename = filenames[filenames.length-1];
//var extension = e[e.length-1];
FileUpload.uploadFile(uploadFile,filename,function(data){
var file_a= document.getElementById("file_a");
file_a.href=data;
file_a.innerHTML=data;
document.getElementById("filediv").style.display="";
});
}
</script>
</head>
<body>
<table border="1" cellpadding="3" width="50%">
<tr>
<td>Image</td>
<td><input type="file" id="uploadImage" /></td>
<td><input type="button" onclick="uploadFiles()" value="upload"/><div id="image.container"> </div></td>
</tr>
<tr>
<td>File</td>
<td><input type="file" id="uploadFile" /></td>
<td><input type="button" onclick="uploadFile()" value="upload"/><div id="file.container"> </div></td>
</tr>
<tr>
<td colspan="3"></td>
</tr>
</table>
<img id="image" src="javascript:void(0);"/>
<div id="filediv" style="display:none;">
<a href="" id="file_a">上传的文件</a>
</div>
</body>
</html>
添加进度条么,就需要用reverse ajax 进行配合使用了。
- 大小: 34.7 KB
分享到:
相关推荐
包涵了DWR3.0的JAR包,DWR3.0的说明文档,通俗易懂。
DWR3.0 全部文件 包括文档 很实用哦
dwr dwr包 dwr3.0文档 dwr3.0.jar
DWR3.0官方中文入门教程,和dwr3.0安装文件。
dwr3.0的web.xml的配置文件,该文件是从dwr.jar文件中拷贝出来的
这个项目中,详细介绍了dwr3.0+SSH2的配置,包含全部jar包及sql文件
dwr3.0.jar
最新dwr3.0的包,有需要的朋友就下载吧
dwr3.0以及相关中文文档
dwr3.0文档和jar包。文档是英文原版。
DWR3.0.jar、DWR的实例war和DWR2.0中文说明文档
dwr3.0的搭建与基本的介绍,希望可以给初学者带来帮助
dwr3.0.zip包括dwr3.0的jar包和war运行文件,方便下载
手把手教你实现EXTJS+DWR实现图片上传功能
DWR学习资料 :DWR 3.0 上传文件.txt DWR3.0反向Ajax示例.txt DWR3.0学习笔记.txt DWR3.0学习网址.txt dwr分页.doc DWR分页代码.doc DWR中文文档.doc DWR中文文档.pdf dwr做comet的完整实现.doc Spring整合DWR comet ...
网上的资源很多都是2.0的,将dwr官网的Clock实现,简单明了。是使用反向Ajax的好用的参考例子,配置文件全。
NULL 博文链接:https://liuningbo.iteye.com/blog/1131111
去年翻译了一部分,放在硬盘里,一直没拿出来。工作了,只能先把它放这搁一下了. java流行框架. 希望热心的朋友联系我,一起交流,完成她的翻译工作, 博客 http://hi.baidu.com/passedbylove qq:13019940
dwr3.0.rar