ITeye博客

swfupload+Struts2多文件上传

qqybh · 2009-10-10 08:44 · 编程语言 · CSS · Struts · Flash · OS · JSP · Markdown

公司做了一个相册模块,其中有一个要用多文件上传,可是使用的是很老的单文件上传的模式,感觉使用不是很方便,于是在网上搜索了一下,搜到了swfupload这个上传组件,于是尝试着使用struts2整合swfupload写了写,现在把代码放上来。 一、起始页代码 [code="java"] <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %>

SWFUpload

v2.2.0

Multi-Instance Demo

This page demonstrates how multiple instances of SWFUpload can be loaded on the same page. It also demonstrates the use of the graceful degradation plugin and the queue plugin.

Large File Upload Site

Small File Upload Site

[/code] 二、upload.jsp [code="java"] <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%@ taglib prefix="s" uri="/struts-tags" %> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> [/code] 三、action文件 [code="java"] package com.action; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.InputStream; import java.io.OutputStream; import org.apache.struts2.ServletActionContext; import com.model.Picture; import com.opensymphony.xwork2.ActionSupport; public class PictureAction extends ActionSupport { /** * */ private static final long serialVersionUID = 1L; private File file; private String fileFileName; private String fileContentType; public File getFile() { return file; } public void setFile(File file) { this.file = file; } public String getFileFileName() { return fileFileName; } public void setFileFileName(String fileFileName) { this.fileFileName = fileFileName; } public String getFileContentType() { return fileContentType; } public void setFileContentType(String fileContentType) { this.fileContentType = fileContentType; } /** * 用于实现上传功能 */ @Override public String execute() throws Exception { // TODO Auto-generated method stub //实现上传 InputStream is = new FileInputStream(file); String root = ServletActionContext.getRequest().getRealPath("/upload"); System.out.println(this.getFileContentType() ); File deskFile = new File(root,this.getFileFileName()); OutputStream os = new FileOutputStream(deskFile); byte [] bytefer = new byte[400]; int length = 0 ; while((length = is.read(bytefer) )>0) { os.write(bytefer,0,length); } os.close(); is.close(); return SUCCESS; } } [/code] 上传不上去的原因的是没有新建一个文件夹 upload

评论(9)

  1. #9 楼 · ahhmdwg · 2014-11-29 16:19

    inde.jsp的是多文件上传,看错了不好意思! 谢谢

  2. #8 楼 · ahhmdwg · 2014-11-29 15:54

    这哪是多文件上传啊,upload.jsp就一个文件上传,坑你爹啊

  3. #7 楼 · 13776349315 · 2014-05-28 16:29

    很棒,值得学习,在根目录新建一个upload文件夹就能上传成功了,不过queue有限制不知道在哪里控制,我再看看~谢谢LZ的分享

  4. #6 楼 · bobommmm · 2013-10-28 14:45

    这个upload文件夹是应该建在哪里呢?

  5. #5 楼 · zhuange · 2013-10-21 18:04

    ** :idea: **

  6. #4 楼 · qqybh · 2012-12-14 15:18

    个重action返回的是什么东西? 如果后台出现什么情况

    q412239930 writes:

    你这个重action返回的是什么东西? 如果后台出现什么情况没有上传成功,怎么样返回数据到前台?

    这个只是一个主体代码,action返回的SUCCESS是struts2的基本配置而已。。这个没有异常处理

  7. #3 楼 · q412239930 · 2012-06-11 15:13

    你这个重action返回的是什么东西? 如果后台出现什么情况没有上传成功,怎么样返回数据到前台?

  8. #2 楼 · luger · 2012-05-15 18:00

    :idea: :idea: :idea: :idea: :idea:

  9. #1 楼 · ZoeXin · 2010-11-10 16:24

    :idea:

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