ITeye博客

webwork/Struts 2 实现页面无刷新多文件上传

nello · 2011-07-28 10:11 · webwork · Struts 2 · 行业应用 · Java · Struts · Webwork · Markdown

这个demo是用webwork实现的,原理上和用Struts 2完全一样。实现多文件上传和页面无刷新效果,用iframe技术实现了页面无刷新功能。上传成功后,后台向前台反馈上传成功的文件信息。 UploadAction.java 实现文件上传功能的action类。 [code="java"] package net.androidla.action; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.PrintWriter; import java.util.ArrayList; import java.util.List; import javax.servlet.http.HttpServletResponse; import com.opensymphony.webwork.ServletActionContext; import com.opensymphony.xwork.ActionSupport; public class UploadAction extends ActionSupport { private static final long serialVersionUID = 1L; private File[] file; private String[] filename; private String name; private String msg; public String[] getFilename() { return filename; } public void setFilename(String[] filename) { this.filename = filename; } public String getMsg() { return msg; } public void setMsg(String msg) { this.msg = msg; } public File[] getFile() { return file; } public void setFile(File[] file) { this.file = file; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String upload() { StringBuilder str = new StringBuilder(); List filenamearr = new ArrayList(); for (int n = 0; n < filename.length; n ++) { if (!filename[n].equals("")) { filenamearr.add(filename[n]); } } File path = new File("C:/uploaddir"); if (!path.exists()) { path.mkdirs(); } FileOutputStream fos = null; FileInputStream fis = null; byte[] b = new byte[1024 * 10]; int temp = 0; String name = null; String fname = null; int f = 0; for (int i = 0; i < file.length; i ++) { try { fname = filenamearr.get(i); f = fname.lastIndexOf("\\"); name = f != -1 ? fname.substring(f + 1) : fname; fis = new FileInputStream(file[i]); fos = new FileOutputStream(path + File.separator + name); while ((temp = fis.read(b, 0, b.length)) != -1) { fos.write(b, 0, temp); } fos.flush(); fos.close(); fis.close(); str.append(name + " 上传成功 "); } catch (Exception e) { e.printStackTrace(); } } try { HttpServletResponse res = ServletActionContext.getResponse(); res.setCharacterEncoding("UTF-8"); PrintWriter out = res.getWriter(); out.println(""); } catch (IOException e) { e.printStackTrace(); } return NONE; } } [/code] webwork的配置文件。 [code="xml"] 2048000 image/bmp,image/png,image/gif,image/jpeg,image/x-png,image/pjpeg,application/x-zip-compressed [/code] 前台jsp文件: [code="java"] <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%@ taglib uri="/webwork" prefix="ww" %> <%@ page import="com.opensymphony.xwork.ActionContext" %> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> My JSP 'index.jsp' starting page

支持"jpg", "jpeg", "gif", "zip"文件的上传

[/code] [img]http://dl.iteye.com/upload/attachment/525940/8ed7637b-bb99-3b36-a6c9-e6e9038a5f3e.png[/img]

评论(3)

  1. #3 楼 · longmain · 2011-11-03 11:00
     out.println("<script type=\"text/javascript\">parent.callback('" + str.toString() + "')</script>");   
    

    这个会在struts处理完写回页面上的hidden frame里。那时候调用。

  2. #2 楼 · nello · 2011-08-23 12:04

    ydpiaoyun writes:

    你好,请问function callback(msg) 这个回调函数在哪调用了,怎么没找到调用的地方呢。谢谢! QQ:495231441

    out.println("<script type="text/javascript">parent.callback('" + str.toString() + "')");

  3. #1 楼 · ydpiaoyun · 2011-08-23 12:00

    你好,请问function callback(msg) 这个回调函数在哪调用了,怎么没找到调用的地方呢。谢谢! QQ:495231441

评论功能维护中,当前仅展示历史评论。