0 0

swfupload+struts2问题0

网上找到一个很好的例子:关于swfupload的,一直听说很好使。就试了试。用完感觉确实强大,后来发现是struts1版本的。所以打算改到struts2版本的。问题是上传完了以后,页面显示的是成功,可是没有存到指定文件夹中,都没有上传上来。

strus2代码片段
<action name="uploadAction" class="com.action.UploadAction"
			method="forUpload">
			<result name="success">index.jsp</result>
		</action>


Action中代码片段--这个我是模仿人家写的,肯定有错,但是不知道哪有问题
package com.action;

import java.io.File;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;

@SuppressWarnings("serial")
public class UploadAction extends ActionSupport {

	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;
	}

	@SuppressWarnings("unchecked")
	public String forUpload() {
		try {
			HttpServletRequest request = ServletActionContext.getRequest();
			request.setCharacterEncoding("UTF-8");
			FileItemFactory factory = new DiskFileItemFactory();
			ServletFileUpload upload = new ServletFileUpload(factory);
			// upload.setHeaderEncoding(request.getCharacterEncoding());
			// System.out.println("request:" + request);
			List items = upload.parseRequest(request);

			Iterator iter = items.iterator();
			while (iter.hasNext()) {
				FileItem item = (FileItem) iter.next();

				if (item.isFormField()) {/*
										 * String name = item.getFieldName();
										 * String value = item.getString();
										 * System.out.println("name = " + name);
										 * System.out.println("value = " +
										 * value); System.out.println("/r/n");
										 */
				} else {
					String fieldName = item.getFieldName();

					String fileName = item.getName();
					System.out.println("文件类型   "
							+ fileName.substring(fileName.lastIndexOf('.') + 1,
									fileName.length()));
					// boolean isInMemory = item.isInMemory();
					long sizeInBytes = item.getSize();
					getListInSession(request).add(fileName);
					getListInSession(request).add(UUID.randomUUID());

					// String[] str=fieldName.split("\\.");
					// int l=str.length-1;
					// System.out.println("文件类型 "+str[l]);
					//					
					System.out.println("fileName = " + fileName);
					// System.out.println("文件存储路径
					// "+request.getRealPath("/")+"uploadfile/");
					// 路径 D:\Program
					// Files\apache-tomcat-6.0.18\webapps\rockontrol_oa\UploadFiles\2009042991039312\
					String filePath = ServletActionContext.getServletContext()
							.getRealPath("/")
							+ "uploadfile/";
					File savePath = new File(filePath);
					System.out.println(savePath.getAbsolutePath());
					if (!savePath.exists()) {
						savePath.mkdir();
					}
					item.write(new File(filePath + fileName));

				}
			}
		} catch (Exception ex) {
			ex.printStackTrace();
		}

		return SUCCESS;
	}

	public List getListInSession(HttpServletRequest request) {
		List result = (List) request.getSession().getAttribute("_result");
		if (result == null) {
			result = new ArrayList();
			request.getSession().setAttribute("_result", result);
		}
		return result;
	}
	
}


jsp代码片段:css/js/swf我都有
<%@ page contentType="text/html; charset=GB2312"%>
<!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>
		<meta http-equiv="Content-Type" content="text/html; charset=GBK" />
		<link href="css/default.css" rel="stylesheet" type="text/css" />
		<title>文件上传</title>

		<script type="text/javascript" src="jslib/swfupload.js"></script>
		<script type="text/javascript" src="jslib/swfupload.queue.js"></script>
		<script type="text/javascript" src="jslib/fileprogress.js"></script>
		<script type="text/javascript" src="jslib/handlers.js"></script>
		<script type="text/javascript">
		var swfu;
		window.onload = function() {
			var settings = {
				flash_url : "jslib/swfupload.swf",
				//上传文件路径
				upload_url: "uploadAction.action",
				//上传文件大小
				file_size_limit : "1024 MB",
				//上传文件类型用;分开
				file_types : "*.rmvb;*.flv;*.avi;*.mp4",
				//上传文件描述
				file_types_description : "All Files",
				file_upload_limit : 100,
				//上传文件限制为0不限制
				file_queue_limit : 0,
				custom_settings : {
					progressTarget : "fsUploadProgress",
					uploadButtonId : "btnUpload",
					cancelButtonId : "btnCancel"
				},
				debug: false,
				auto_upload:false,

				// Button settings
				button_image_url: "images/TestImageNoText_65x29.png",
				button_width: "65",
				button_height: "29",
				button_placeholder_id: "spanButtonPlaceHolder",
				button_text: '<span class="theFont">选择</span>',
				button_text_style: ".theFont { font-size: 14; }",
				button_text_left_padding: 15,
				button_text_top_padding: 3,
				
				// The event handler functions are defined in handlers.js
				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,
				queue_complete_handler : queueComplete	// Queue plugin event
			};

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

	<body>
		<form id="form1" action="uploadAction.action" method="post"
			enctype="multipart/form-data">
			<div class="wrap" id="content">
				<div class="titleDiv">
					<div class="titleFont">
						<img src="images/up.png" width="16" height="16" />
						文件上传
					</div>
				</div>
				<div class="content">
					<div class="fieldset flash" id="fsUploadProgress">
						<p>
							<span class="legend">上传列表</span>
						</p>
					</div>
					<div id="divStatus" class="num">
						0 个文件上传
					</div>
					<div style="margin-left: 30px;">
						<span id="spanButtonPlaceHolder"></span>
						<div style="margin-top: -20px; margin-left: 60px;">
						
						<input id="btnUpload" type="button" value="上 传"
									onclick="swfu.startUpload();"
									style="margin-left: 20px; margin-bottom: 10px" class="Btn" />
									
							<input id="btnCancel" type="button" height="29" value="取消上传"
								onclick="swfu.cancelQueue();"
								style="margin-left: 20px; margin-bottom: 10px" />
						</div>
					</div>
				</div>
			</div>
		</form>
	</body>
</html>


谢谢帮助
2013年11月07日 15:17

1个答案 按时间排序 按投票排序

0 0

这代码写的多弱智啊

2013年11月08日 11:28

相关推荐

Global site tag (gtag.js) - Google Analytics