struts2+swfupload多文件上传

阅读更多
上传页面代码
swfupload参数网上都有,可以查一下

upload_url: "uploadAction.action"//上传文件所请求的action

file_post_name:接收文件的aciton属性
在action中还要加上*FileName,*就是file_post_name你所设置的,然后设置setget方法。

button开头的是设置按钮属性

uploadsuccess等是设置回调,都在handler.js中.


<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>



 
 
  
  
   
   
  
  
 
 
 

 

File Upload


   

   

文件上传









上传文件列表




disabled="disabled" style="margin-left: 2px; height: 22px; font-size: 8pt;" />





   

   

 



action代码
package com.xpec.action;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;

import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.components.Form;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.xpec.enums.UploadState;

public class FileUpLoadAction extends ActionSupport{

/**
*
*/
private static final long serialVersionUID = 1L;

private File fileData;

    private String fileDataFileName;
   
    private String fileDataContentType;

public String getFileDataContentType() {
return fileDataContentType;
}



public void setFileDataContentType(String fileDataContentType) {
this.fileDataContentType = fileDataContentType;
}



public File getFileData() {
return fileData;
}



public void setFileData(File fileData) {
this.fileData = fileData;
}



public String getFileDataFileName() {
return fileDataFileName;
}



public void setFileDataFileName(String fileDataFileName) {
this.fileDataFileName = fileDataFileName;
}

public String fileUpLoad() throws IOException{
InputStream is = new FileInputStream(fileData);
File deskFile = new File("D:/upLoadFile/fileUp",this.getFileDataFileName());
OutputStream os = new FileOutputStream(deskFile);
byte [] bytefer = new byte[1024];
int length = 0 ;
while((length = is.read(bytefer) )>0)
{
os.write(bytefer,0,length);
}
os.close();
is.close();
return "success";
}
}


struts.xml

/index.jsp

上传成功之后会找寻index.jsp也就是你配置的,不回调转但是会找,会跳到handler.js里的uploadsuccess,serverdata就是index.jsp内容你需要用正则表达式取出相应的数据来

判断是否上传成功,弹出相应提示。

fileupload.jsp,fileuploadaction.action, index.jsp这个三文件是上传代码。其他的只是随意的测试。[size=large]
[/size]
  • testjavascript.jar (67 KB)
  • 下载次数: 66

你可能感兴趣的:(struts2,swfupload,多文件上传)