dwr上传文件

1.导入commons-fileupload-1.2.2.jar, commons-io-2.2.jar,dwr.jar包到lib里面

2.配置XML文件

  1. web.xml 里面的代码

<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>week17_dwrdisplay-name>

  
  <listener>
    <listener-class>
        org.directwebremoting.servlet.DwrServlet
    listener-class>
  listener>

  <servlet>
    <servlet-name>dwrservlet-name>
    <servlet-class>
        org.directwebremoting.servlet.DwrServlet
    servlet-class>
    <init-param>
             <param-name>crossDomainSessionSecurityparam-name>
             <param-value>falseparam-value>
      init-param>
      <init-param>
            <param-name>allowScriptTagRemotingparam-name>
            <param-value>trueparam-value>
      init-param>
  servlet>

  <servlet-mapping>
    <servlet-name>dwrservlet-name>
    <url-pattern>/dwr/*url-pattern>
  servlet-mapping>








  <welcome-file-list>
    <welcome-file>index.htmlwelcome-file>
    <welcome-file>index.htmwelcome-file>
    <welcome-file>index.jspwelcome-file>
    <welcome-file>default.htmlwelcome-file>
    <welcome-file>default.htmwelcome-file>
    <welcome-file>default.jspwelcome-file>
  welcome-file-list>
web-app>
  1. 在web.xml同目录下创建dwr.xml文件,代码如下


<dwr>

    <allow>
        <filter class="org.directwebremoting.filter.AuditLogAjaxFilter" />

        
        

        <create creator="new" javascript="FileService_js">
            <param name="class" value="com.it.bean.FileService">param>
            <include method="upload"/>
        create>

    allow>



dwr>

3.Javabean类代码

在src目录下面创建com.it.bean.FileService.java类

package com.it.bean;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

import org.apache.commons.io.FilenameUtils;
import org.directwebremoting.WebContext;
import org.directwebremoting.WebContextFactory;

public class FileService {

     public String upload(InputStream is, String fileName) throws IOException{
         //获取相应的ServletAPI
         WebContext wc = WebContextFactory.get();
         //对应需要在webapp目录下面创建upload文件夹
         String realPath = wc.getSession().getServletContext().getRealPath("/upload");
         //文件名
         String fn = FilenameUtils.getName(fileName);

        /* System.out.println(realPath);
         System.out.println(realPath + "\\" + fn);*/
         //创建文件
         File file = new File(realPath + "\\" + fn);


         InputStream uploadFile = is; 
         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 fn;
     }
}

4.jsp代码

index.jsp里面的代码

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>


<html>
  <head>

    <title>My JSP 'index.jsp' starting pagetitle>
    <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>
    <input type="file" id="file" name="file" value="请选择需要上传的文件"/>   
    <button id="btn" onclick="uploadFile()">上传文件button>

  body>

  
    <script type="text/javascript" src="dwr/interface/FileService_js.js">script>
    <script type="text/javascript" src="dwr/engine.js">script>
    <script type="text/javascript" src="dwr/util.js">script>

    
    <script type="text/javascript">
        function uploadFile(){
            var file = dwr.util.getValue("file");
            alert(file.value);
            FileService_js.upload(file, file.value, function(result){
                alert(1);
                alert(result);
            });
        };


    script>
html>

在webroot目录下面创建一个新文件夹upload,文件上传到tomcat目录下面的

你可能感兴趣的:(java)