zoukankan      html  css  js  c++  java
  • dwr文件上传

    配置FileService映射:

    dwr.xml

    <create creator="new">
          <param name="class" value="com.demo.service.FileService"/>
        </create>


    FileService:

    package com.demo.service;
    
    import java.io.File;
    import java.io.IOException;
    import java.io.InputStream;
    
    import org.apache.commons.io.FileUtils;
    import org.apache.commons.io.FilenameUtils;
    import org.directwebremoting.WebContext;
    import org.directwebremoting.WebContextFactory;
    
    public class FileService {
    	public String upload(InputStream inputStream,String fileName) throws IOException{
    		String tempFileName=FilenameUtils.getName(fileName);
    		String path=getRealPath("upload");
    		File file=new File(path+File.separator+tempFileName);
    		FileUtils.copyInputStreamToFile(inputStream, file);
    		return file.getPath();
    	}
    	public String getRealPath(String dir){
    		WebContext context=WebContextFactory.get();
    		return context.getSession().getServletContext().getRealPath(dir);
    	}
    }
    


    upload.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>文件上传</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/FileService.js"></script>
    <script type="text/javascript">
    	function upload(){
    		var file=dwr.util.getValue("file");
    		FileService.upload(file,file.value,function(result){
    			alert(result);
    		});
    	}
    </script>
    </head>
    <body>
    附件:<input type="file" id="file" name="file"><br/>
    <button onclick="upload();">上传</button>
    </body>
    </html>


    结果:

    附件已上传到upload目录中



  • 相关阅读:
    进程 触发器
    关于 if条件 光标 循环的综合应用
    3-15记录
    day3.python 学习之列表
    day2: python3.5学习——逻辑判断
    day1: python3.5学习
    OpenGL_曲线函数
    OpenGL_赛平斯基垫片
    【quick-cocos2d-x 游戏开发之一】开发工具sublime text及其强力插件QuickXDev
    Python正则表达式指南
  • 原文地址:https://www.cnblogs.com/liguangsunls/p/7270098.html
Copyright © 2011-2022 走看看