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" onclick="swfu.startUpload();" disabled="disabled" />
            <input id="btnCancel1" type="button" value="取消" onclick="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;
    }

}
 

你可能感兴趣的:(Struts+swfupload实现文件上传功能)