67,538
社区成员
发帖
与我相关
我的任务
分享package com.ml.web.action;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import org.apache.struts2.ServletActionContext;
import com.googlecode.jsonplugin.annotations.JSON;
import com.opensymphony.xwork2.ActionSupport;
public class Ext2UploadAction extends ActionSupport {
/**
*
*/
private static final long serialVersionUID = -8217750317798955984L;
private File file1;
private String file1ContentType;
private String file1FileName;
private String savePath1;
private boolean jsonString;
@JSON(name="success")
public boolean getJsonString() {
return jsonString;
}
public void setJsonString(boolean jsonString) {
this.jsonString = jsonString;
}
public File getFile1() {
return file1;
}
public void setFile1(File file1) {
this.file1 = file1;
}
public String getFile1ContentType() {
return file1ContentType;
}
public void setFile1ContentType(String file1ContentType) {
this.file1ContentType = file1ContentType;
}
public String getFile1FileName() {
return file1FileName;
}
public void setFile1FileName(String file1FileName) {
this.file1FileName = file1FileName;
}
public String getSavePath1() {
return ServletActionContext.getRequest().getRealPath(savePath1);
}
public void setSavePath1(String savePath1) {
this.savePath1 = savePath1;
}
public String execute()throws Exception{
String fileName = getFile1FileName();
try{
if(!(new File(getSavePath1()).isDirectory()))
{
new File(getSavePath1()).mkdir();
}
}
catch(SecurityException e)
{
this.LOG.debug(e.getMessage());
}
FileOutputStream fos = new FileOutputStream(getSavePath1() + "/" + fileName);
FileInputStream fis = new FileInputStream(getFile1());
byte[] buffer = new byte[1024];
int len = 0;
while((len = fis.read(buffer)) > 0){
fos.write(buffer,0,len);
}
fis.close();
fos.close();
this.jsonString = true;;
return SUCCESS;
}
}<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<package name="ext2" extends="json-default" >
<action name="ext2Upload" class="com.ml.web.action.Ext2UploadAction">
<param name="savePath1">/upload</param>
<result name="success" type="json">
<param name="excludeProperties">file1,file1ContentType,file1FileName,savePath1</param>
<param name="exportVars">success</param>
<param name="contentType">text/html</param>
</result>
</action>
</package>
</struts> Ext.onReady( function (){
var form = new Ext.form.FormPanel({
labelAlign: 'right',
title: 'form',
labelWidth: 50,
frame:true,
fileUpload: true,
url:" ext2Upload.action",
width: 380,
items: [{
xtype: 'textfield',
fieldLabel: '文本框',
name: 'file1',
inputType: 'file'//文件类型
}],
buttons: [{
text: '保存',
handler: s
}]
});
function s() {
form.getForm().submit({//客户端的数据提交给服务器
waitTitle:"请稍候",
waitMsg:"正在提交表单数据,请稍候。。。。。。",
//如果submit失敗,執行這一個function
failure:function(){
Ext.MessageBox.hide();
Ext.MessageBox.alert('Error','失败');
},
success: function(){
Ext.MessageBox.hide();
Ext.MessageBox.alert('Success','成功');
}
});
}
form.render("fileUpload");
});
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link rel="stylesheet" type="text/css"
href="ext/resources/css/ext-all.css" />
<script type="text/javascript" src="ext/adapter/ext/ext-base.js"></script>
<script type="text/javascript" src="ext/ext-all.js"></script>
<script type="text/javascript" src="ext/ext-lang-zh_CN.js"></script>
<script type="text/javascript" src="js/upload.js"></script>
</head>
<body>
<div id="fileUpload"></div>
</body>
</html>