---
title: "swfupload+Struts2多文件上传"
author: "qqybh"
date: "2009-10-10T08:44:15Z"
tags: ["CSS", "Flash", "JSP", "OS", "Struts"]
categories: ["编程语言"]
slug: "swfupload-struts2"
draft: false
lastmod: "2010-03-11T09:58:22Z"
id: 484098
url: "https://qqybh.iteye.com/blog/484098"
---

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

# [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"] &lt;%@ page language="java" import="java.util.\*" pageEncoding="UTF-8"%&gt; &lt;%@ taglib prefix="s" uri="/struts-tags" %&gt; &lt;% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %&gt; \[/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) )&gt;0) { os.write(bytefer,0,length); } os.close(); is.close(); return SUCCESS; } } \[/code] 上传不上去的原因的是没有新建一个文件夹 upload