flex上传文件

flex端的AS代码:

internal function init():void{    
Security.allowDomain("*");
file = new FileReference();
file.addEventListener(Event.SELECT, onSelect);
file.addEventListener(ProgressEvent.PROGRESS, processHandler);
file.addEventListener(IOErrorEvent.IO_ERROR,ioShow);
}
internal function ioShow(evt: IOErrorEvent){
Alert.show(evt.toString(),"IO错误");
}
internal function doSelect():void{
//文件类型限制,第一个是文件类型选项只显示"png"类型的,第二个是只把.png文件显示出来
var imageTypes:FileFilter = new FileFilter("png", "*.png");
var allTypes:Array = new Array(imageTypes);
file.browse(allTypes);
}
internal function onSelect(evt:Event):void{
pic_txt.text = file.name;
}
internal function doUpload():void{
//指向struts的一个action,或servlte,注意一写要写完整路径
var request:URLRequest = new URLRequest("http://localhost:8080/productManage/uploadPic.do");

fileName = "productImage\\"+new Date().getTime().toString() +".png";
request.data = new URLVariables("filename="+fileName);
file.upload(request);
CursorManager.setBusyCursor();
}
//上传完成更新源
internal function processHandler(evt:ProgressEvent):void{
if(evt.bytesLoaded == evt.bytesTotal){

CursorManager.removeBusyCursor();

var n:Number = new Date().getTime();
img.source = fileName +"?time="+n;
}
}
<mx:TextInput id="pic_txt" width="150"/>
<mx:Button label="选择文件" click="doSelect()"/>
<mx:Button label="开始上传" click="doUpload()"/>



internal function init():void{
Security.allowDomain("*");
file = new FileReference();
file.addEventListener(Event.SELECT, onSelect);
file.addEventListener(ProgressEvent.PROGRESS, processHandler);
file.addEventListener(IOErrorEvent.IO_ERROR,ioShow);
}
internal function ioShow(evt: IOErrorEvent){
Alert.show(evt.toString(),"IO错误");
}
internal function doSelect():void{
//文件类型限制,第一个是文件类型选项只显示"png"类型的,第二个是只把.png文件显示出来
var imageTypes:FileFilter = new FileFilter("png", "*.png");
var allTypes:Array = new Array(imageTypes);
file.browse(allTypes);
}
internal function onSelect(evt:Event):void{
pic_txt.text = file.name;
}
internal function doUpload():void{
//指向struts的一个action,或servlte,注意一写要写完整路径
var request:URLRequest = new URLRequest("http://localhost:8080/productManage/uploadPic.do");

fileName = "productImage\\"+new Date().getTime().toString() +".png";
request.data = new URLVariables("filename="+fileName);
file.upload(request);
CursorManager.setBusyCursor();
}
//上传完成更新源
internal function processHandler(evt:ProgressEvent):void{
if(evt.bytesLoaded == evt.bytesTotal){

CursorManager.removeBusyCursor();

var n:Number = new Date().getTime();
img.source = fileName +"?time="+n;
}
}
<mx:TextInput id="pic_txt" width="150"/>
<mx:Button label="选择文件" click="doSelect()"/>
<mx:Button label="开始上传" click="doUpload()"/>

java端:

import java.io.File;    
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.fileupload.DiskFileUpload;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
public class UploadPicAction extends Action {
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
response.setContentType("text/html; charset=UTF-8");
DiskFileUpload upload = new DiskFileUpload();
try {
List itemlist = upload.parseRequest(request);
//itemlist里包含多个参数,所以要判断一下是文件类型,还是参数字段,这里修改一下可以用于多个文件上传
for (int i = 0; i < itemlist.size(); i++) {
FileItem item = (FileItem) itemlist.get(i);
if (item.isFormField())[color=red]//是表单字段跳过 [/color]
continue;
String name = request.getRealPath("") + "[url=file://\\bin\\]\\bin\\[/url]"
+ request.getParameter("filename");//获得Web应用绝对路径,如果路径不在web应用下,由于安全机制,将会说"找不到系统指定路径"

try {
File f = new File(name);
item.write(f);
} catch (Exception e) {
e.printStackTrace();
}
}
} catch (FileUploadException e) {
e.printStackTrace();
}
return null;
}
}


转自[url]http://www.iteye.com/topic/233248[/url]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值