dwr檔案上傳,dwr2檔案上傳

來源:互聯網
上載者:User

dwr檔案上傳,dwr2檔案上傳

配置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檔案夾中




dwr+ajax實現檔案上傳

選中一個檔案後,不是會有檔案的地址嘛,你把這個參數通過AJAX傳到後台,讓後台file.length()
方法將長度返回來不就可以了~
 
dwr圖片上傳

我拿dwr寫了一個網站,在本機測試都好使,但是一上傳到java空間上就說 IE暈死,這是檔案名稱未指定造成的。 <script type="text/javascript" src="
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.