cAng^Er

不懂我的人 , 离不了我 , 该了解了解我 !而懂我的人 , 更离不了我 , 因为他们爱我 。

  BlogJava :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理 ::
  30 随笔 :: 16 文章 :: 18 评论 :: 0 Trackbacks

 用webwork框架做upload的java读取代码
    我的语言表达能力不是很好,说也许说不明白,可能给看的说得糊涂了,同样
这个对做上传得新手很有用,不啰嗦了,先写代码吧:一共4个地方要弄,
前台,后台,xwork和webwork.properties
前台:
upload.jsp:
=================================================================
<%--
  Created by IntelliJ IDEA.
  User: lvjiachun
  Date: 2006-5-3
  Time: 9:32:35
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=utf-8" language="java" %>
<%@ taglib prefix="ww" uri="/webwork" %>
<html>
<head><title>upload</title></head>
<body><p align="center">
    <ww:form action="upload.action" name="upload" method="post" enctype="multipart/form-data">
        <ww:file name="file1" value="浏览" label="ReadFile"/>
        <ww:file name="file2" value="浏览" label="ReadFile"/>
        <ww:file name="file3" value="浏览" label="ReadFile"/>
        <ww:file name="file4" value="浏览" label="ReadFile"/>
        <ww:textfield name="model.fileURL" label="Road"/>  //指定上传得文件保存到哪里
        <ww:submit/>
    </ww:form></p>
</body>
</html>
=======================================================================
upload-success.jsp
=======================================================================
<%--
  Created by IntelliJ IDEA.
  User: lvjiachun
  Date: 2006-5-3
  Time: 9:38:04
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=utf-8" language="java" %>
<%@ taglib prefix="ww" uri="/webwork" %>
<html>
<head><title>upload-success</title></head>

<body>
<p align="center">
    你已经上传成功,
    <a href="upload.jsp">请返回</a>
</p>
</body>
</html>
========================================================================
后台:一共需要三个4个java文件
1.  <FileMes>
package upload.until;

/**
 * Created by IntelliJ IDEA.
 * User: lvjiachun
 * Date: 2006-5-3
 * Time: 7:51:11
 * To change this template use File | Settings | File Templates.
 */
public class FileMes {
    private String fileName;

    public String getFileName() {
        return fileName;
    }

    public void setFileName(String fileName) {
        this.fileName = fileName;
    }

    public String getFilePath() {
        return filePath;
    }

    public void setFilePath(String filePath) {
        this.filePath = filePath;
    }

    private String filePath;
}
2.<FileSelect >
package upload.until;

/**
 * Created by IntelliJ IDEA.
 * User: lvjiachun
 * Date: 2006-5-3
 * Time: 7:51:23
 * To change this template use File | Settings | File Templates.
 */
public class FileSelect {
    private String fileName;
    private String fileURL;
    private String type;

    public String getFileName() {
        return fileName;
    }

    public void setFileName(String fileName) {
        this.fileName = fileName;
    }

    public String getFileURL() {
        return fileURL;
    }

    public void setFileURL(String fileURL) {
        this.fileURL = fileURL;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }
}
3.<UploadUntil >
package upload.until;

import com.opensymphony.webwork.dispatcher.multipart.MultiPartRequestWrapper;
import com.opensymphony.webwork.dispatcher.multipart.MultiPartRequest;
import com.opensymphony.webwork.ServletActionContext;

import java.util.List;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.Calendar;
import java.io.*;

/**
 * Created by IntelliJ IDEA.
 * User: lvjiachun
 * Date: 2006-5-3
 * Time: 7:51:00
 * To change this template use File | Settings | File Templates.
 */
public class UploadUntil {
    public static List<FileMes> Upload(String smallPath) throws IOException {
        List<FileMes> fileMesList = new ArrayList<FileMes>();
        String path = smallPath;
        System.out.println("PATH 2:" + path);
        MultiPartRequestWrapper multiWrapper = (MultiPartRequestWrapper) ServletActionContext.getRequest();
        if (multiWrapper == null) return null;
        Enumeration e = multiWrapper.getFileNames();
        int i = 0;
//===============================================================//
        //这个是单文件上传的代码;!!如果想做单文件上传,把注释掉的代码去掉就可以了
//        while (e.hasMoreElements()) {
//            String inputValue = (String) e.nextElement();
//            String contentType = multiWrapper.getContentType(inputValue);
//            String flie1 = multiWrapper.getFilesystemName(inputValue);
//            String fileName = flie1;
//            System.out.println("FileName underfinded:" + fileName);
//            String savePath = path + fileName;
//            File file = multiWrapper.getFile(inputValue);
//            if (file != null) {
//                InputStream inStream = new FileInputStream(file.getPath());
//                FileMes fm = new FileMes();
//                fm.setFileName(flie1);
//                System.out.println("FileName:" + fm.getFileName());
//                System.out.println("SmallPath in UploadUntil.java" + smallPath);
//                fm.setFilePath(smallPath + fileName);
//                System.out.println("FilePath:" + fm.getFilePath());
//                fileMesList.add(fm);
//                System.out.println("SavePath:" + savePath);
//                FileOutputStream fs = new FileOutputStream(savePath);
//                byte[]buffer = new byte[1444];
//                int length;
//                int bytesum = 0;
//                int byteread = 0;
//                while ((byteread = inStream.read(buffer)) != -1) {
//                    bytesum += byteread;
//                    fs.write(buffer, 0, byteread);
//                }
//============================================================//
// 下面这个是多文件上传-------------------
        while (e.hasMoreElements()) {
            String inputValue = (String) e.nextElement();
            String contentType = multiWrapper.getContentType(inputValue);
            String flie1 = multiWrapper.getFilesystemName(inputValue);
            String fileName = Calendar.getInstance().getTime().getTime() + "-" + i + flie1;
            String savePath = path + fileName;
            File file = multiWrapper.getFile(inputValue);
            if (file != null) {
                InputStream inStream = new FileInputStream(file.getPath()); //读入原文件
                FileMes fm = new FileMes();
                fm.setFileName(flie1);
                fm.setFilePath(smallPath + fileName);
                fileMesList.add(fm);
                FileOutputStream fs = new FileOutputStream(savePath);
                byte[] buffer = new byte[1444];
                int length;
                int bytesum = 0;
                int byteread = 0;
                while ((byteread = inStream.read(buffer)) != -1) {
                    bytesum += byteread; //字节数 文件大小
                    fs.write(buffer, 0, byteread);
                }
//==============================================================//
                inStream.close();   //关闭输入流
                i++;
                if (file.exists()) {
                    file.delete();
                }   //如果上传的文件存在则将其删除;
            }

        }
        return fileMesList;
    }
}
4.也就是最后一个,action<UploadfilesAction>
package upload.fileupload;

import com.opensymphony.xwork.ActionSupport;
import com.opensymphony.xwork.Action;
import com.opensymphony.xwork.ModelDriven;
import upload.until.FileSelect;

/**
 * Created by IntelliJ IDEA.
 * User:lvjiachun
 * Date: 2006-5-3
 * Time: 7:52:34
 * To change this template use File | Settings | File Templates.
 */
public class UploadfilesAction extends ActionSupport implements Action, ModelDriven {
    FileSelect fileMes = new FileSelect();

    public String upload() throws Exception {
        String smallPath = fileMes.getFileURL();
        System.out.println(smallPath+"xiaosao");
        java.io.File myFilePath = new java.io.File(smallPath);
        if (smallPath != null) {
            if (!myFilePath.exists()) {
                myFilePath.mkdir();
                System.out.println("*******MaKe DIR!*********");
            }
        }
        upload.until.UploadUntil.Upload(smallPath);
        return SUCCESS;

    }
    public Object getModel() {
        return fileMes;  //To change body of implemented methods use File | Settings | File Templates.
    }
}
**********************************************************************
xwork中
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE xwork PUBLIC "-//OpenSymphony Group//XWork 1.1.1//EN" "
http://www.opensymphony.com/xwork/xwork-1.1.1.dtd">
<xwork>
    <include file="webwork-default.xml"/>
    <package name="upload" extends="webwork-default">
        <interceptors>
            <interceptor name="upload" class="com.opensymphony.webwork.interceptor.FileUploadInterceptor"/>
            <interceptor-stack name="uploadStack">
                <interceptor-ref name="upload"/>
                <interceptor-ref name="defaultStack"/>
            </interceptor-stack>
        </interceptors>
        <default-interceptor-ref name="WSStack"/>
    
        <action name="upload" class="upload.fileupload.UploadfilesAction" method="upload">
            <result name="success">
                <param name="location">/upload/upload-success.jsp</param>
            </result>
            <interceptor-ref name="uploadStack"/>
            <interceptor-ref name="model-driven"/>
        </action>
    </package>
</xwork>
webwork.properties
在webwork.properties中要加上这段代码
webwork.multipart.parser=com.opensymphony.webwork.dispatcher.multipart.PellMultiPartRequest
webwork.multipart.saveDir=d:\\  #这个是上传文件的默认保存路径#

xiaosao:具体的解析过程不是我写的,是我工作室boss写的,我也没那个能力!具体怎么用
看代码就足够了,这个是我在做上传得时候用到的,开始做的时候没有任何思路,在网上找
也没找到,憋了半个月也没弄出来,用webwork可以得到上传得文件,但传得内容只有upload这
几个字(传txt时候),后来才清楚是要解析过程的,后台的前三段代码就是解析过程,我是个新
手,这个东西做的让我脑袋都大了,为了大家方便,发到这里,有需要的来看一下!代码是好
使得,需要自己认真调试!有什么不足给点意见

posted on 2006-05-11 09:43 cAng^Er 阅读(1256) 评论(2)  编辑  收藏 所属分类: |:Web View:|

评论

# re: 用webwork框架做upload的java读取代码 2006-11-30 14:01 ww
上传的功能好用, 我是想在上传文件的同时,在存入数据库中,怎么不能返回呢? 就是在你的return fileMesList;中去不到文件的名称。为什么,我是新手,谁知道,请告诉我,先谢谢了。  回复  更多评论
  

# re: 用webwork框架做upload的java读取代码 2006-12-05 14:14 cAng^Er
@ww
不好意思阿,好几天没有上博客了,今天在做新闻管理系统,测试友情链接的时候才发现,你把你的联系方式给我,或者看到了加我的QQ:6826462  回复  更多评论
  


只有注册用户登录后才能发表评论。


网站导航: