Struts+swfupload实现文件上传功能

<%@ page contentType="text/html;charset=utf-8" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>SWFUpload Demos - External Interface Demo</title>
<link href="swfupload/default.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="swfupload/swfupload.js"></script>
<script type="text/javascript" src="swfupload/fileprogress.js"></script>
<script type="text/javascript" src="swfupload/handlers.js"></script>
<script type="text/javascript" src="swfupload/jquery.js"></script>
<script type="text/javascript">
    var swfu;
   
    window.onload = function() {
        var settings = {
               flash_url : "<%=request.getContextPath()%>/swfupload/swfupload.swf",
               upload_url: "fileUpload.action",    // Relative to the SWF file
               post_params: { "fileFileName": '<%= request.getParameter("fileFileName") %>' },
               file_size_limit : "100 MB",
               file_types : "*.*",
               file_post_name:"file",//与后台要接受的问价相对应 默认是filedata
               file_types_description : "All Files",
               file_upload_limit : 100,
               file_queue_limit : 0,
               custom_settings : {
                   progressTarget : "fsUploadProgress",
                   cancelButtonId : "btnCancel1",
                   startButton:"startButton",
                   percentage:"percentage"
               },
               debug: false,

               // Button Settings
               button_image_url : "<%=request.getContextPath()%>/swfupload/XPButtonUploadText_61x22.png",    // Relative to the SWF file
               button_placeholder_id : "spanButtonPlaceHolder",
               button_width: 61,
               button_height: 22,
              
               // The event handler functions are defined in handlers.js
               file_dialog_start_handler : fileDialogStart,
               file_queued_handler : fileQueued,
               file_queue_error_handler : fileQueueError,
               file_dialog_complete_handler : fileDialogComplete,
               upload_start_handler : uploadStart,
               upload_progress_handler : uploadProgress,
               upload_error_handler : uploadError,
               upload_success_handler : uploadSuccess,
               upload_complete_handler : uploadComplete
           };

        swfu = new SWFUpload(settings);
    };
</script>
</head>
<body>

<div id="content">
    <form id="form1" method="post" enctype="multipart/form-data">

        <div class="fieldset flash" id="fsUploadProgress">
            <span class="legend">上传队列</span>
        </div>
        <div id="divMovieContainer">
            <span id="spanButtonPlaceHolder"></span> &nbsp;&nbsp;
            <input type="button" value="开始上传" id="startButton" οnclick="swfu.startUpload();" disabled="disabled" />
            <input id="btnCancel1" type="button" value="取消" οnclick="cancelQueue(swfu);" disabled="disabled" />
        </div>   
       
    </form>
</div>
</body>
</html>
 
public class FileUploadAction extends ActionSupport {

    private File file;
    private String fileFileName;

    /**
     * @return the file
     */
    public File getFile() {
        return file;
    }

    /**
     * @param file the file to set
     */
    public void setFile(File file) {
        this.file = file;
    }

    /**
     * @return the fileFileName
     */
    public String getFileFileName() {
        return fileFileName;
    }

    /**
     * @param fileFileName the fileFileName to set
     */
    public void setFileFileName(String fileFileName) {
        this.fileFileName = fileFileName;
    }

    public String upload() {
        InputStream is = null;
        OutputStream os = null;
        try {

            is = new FileInputStream(file);
            String path = ServletActionContext.getServletContext().getRealPath("upload");
            File f = new File(path, fileFileName);
            os = new FileOutputStream(f);
            byte[] buffer = new byte[1024];
            int temp = 0;
            while ((temp = is.read(buffer)) != -1) {
                os.write(buffer, 0, temp);
            }
            System.out.println(fileFileName);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {

            if (is != null) {
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (os != null) {
                try {
                    os.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return SUCCESS;
    }

}
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值