flex+struts2上传文件
来源:互联网 发布:深圳海关数据 编辑:程序博客网 时间:2024/06/10 08:26
刚刚终于解决了flex+struts2上传文件的问题,泪奔。。。。。。。。
都是从网上找来的,勿喷
先看下,flex文件:
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
layout="absolute" xmlns="*" creationComplete="init();">
<mx:Script>
<![CDATA[
import flash.net.FileReference;
import mx.controls.Alert;
import mx.events.CloseEvent;
import flash.events.*;
private var file: FileReference;
private function init(): void
{
Security.allowDomain("*");
file = new FileReference();
file.addEventListener(ProgressEvent.PROGRESS, onProgress);
file.addEventListener(Event.SELECT, onSelect);
file.addEventListener(Event.COMPLETE, completeHandle);
}
private function completeHandle(event:Event):void{
Alert.show("恭喜你,上传成功");
}
private function upload(): void
{
var imageTypes:FileFilter = new FileFilter("Videos (*.mov,*.rmvb,*.flv)", "*.mov;*.rmvb;*.flv");
var allTypes:Array = new Array(imageTypes);
file.browse(allTypes);
file.browse();
}
private function onSelect(e: Event): void
{
Alert.show("上传 " + file.name + " (共 "+Math.round(file.size)+" 字节)?",
"确认上传",
Alert.YES|Alert.NO,
null,
proceedWithUpload);
}
private function onProgress(e: ProgressEvent): void
{
lbProgress.text = " 已上传 " + e.bytesLoaded
+ " 字节,共 " + e.bytesTotal + " 字节";
var proc: uint = e.bytesLoaded / e.bytesTotal * 100;
bar.setProgress(proc, 100);
bar.label= "当前进度: " + " " + proc + "%";
}
private function proceedWithUpload(e: CloseEvent): void
{
if (e.detail == Alert.YES)
{
var request: URLRequest = new URLRequest("FileUpload");
request.contentType="multipart/form-data";
try {
file.upload(request);
}
catch (error:Error)
{
trace("上传失败");
}
}
}
]]>
</mx:Script>
<mx:Canvas width="100%" height="100%" x="10" y="170" fontSize="15">
<mx:VBox width="100%" horizontalAlign="center">
<mx:Label id="lbProgress" text="上传"/>
<mx:ProgressBar id="bar" labelPlacement="bottom"
minimum="0" visible="true" maximum="100" label="当前进度: 0%"
direction="right" mode="manual" width="200"/>
<mx:Button label="上传文件" click="upload();"/>
</mx:VBox>
</mx:Canvas>
</mx:Application>
这个貌似网上很多的,不多说了的。
然后是比较关键的action文件:
public String flexUpLoadFile()
{
MultiPartRequestWrapper multiWrapper =
(MultiPartRequestWrapper) ServletActionContext.getRequest();
Enumeration fileParameterNames = multiWrapper.getFileParameterNames();
while (fileParameterNames != null && fileParameterNames.hasMoreElements())
{
// get the value of this input tag
String inputName = (String) fileParameterNames.nextElement();
// get the content type
String[] contentType = multiWrapper.getContentTypes(inputName);
if (isNonEmpty(contentType)) {
// get the name of the file from the input tag
String[] fileName = multiWrapper.getFileNames(inputName);
if (isNonEmpty(fileName)) {
// Get a File object for the uploaded File
File[] files = multiWrapper.getFiles(inputName);
if (files != null) {
for (int index = 0; index < files.length; index++) {
String uploadPath = "D:\\"+fileName[index];
File dstFile = new File(uploadPath);
this.copy(files[index], dstFile);
}
}
}
}
}
return null;
}
private void copy(File src, File dst) {
InputStream in = null;
OutputStream out = null;
try {
in = new BufferedInputStream(new FileInputStream(src), UserUtil.BUFFER_SIZE);
out = new BufferedOutputStream(new FileOutputStream(dst),
UserUtil.BUFFER_SIZE);
byte[] buffer = new byte[UserUtil.BUFFER_SIZE];
int len = 0;
while ((len = in.read(buffer)) > 0) {
out.write(buffer, 0, len);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (null != in) {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (null != out) {
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
private static boolean isNonEmpty(Object[] objArray) {
boolean result = false;
for (int index = 0; index < objArray.length && !result; index++) {
if (objArray[index] != null) {
result = true;
}
}
return result;
}
struts.xml部分:
<action name="FileUpload" class="com.shangdyu.action.VideoAction" method="flexUpLoadFile">
<result>/UpLoad.jsp</result>
</action>
flexUpLoadFile这个函数貌似比较关键,我试过很多的版本都不行,这边可能是这个函数起的作用,先记录下来吧。
MultiPartRequestWrapper multiWrapper =
(MultiPartRequestWrapper) ServletActionContext.getRequest();
Enumeration fileParameterNames = multiWrapper.getFileParameterNames();
它接收到了从flex传过来的文件的内容。
这个真的搞了好久,原来想用blazeds传送文件,但是经过尝试后发现,简直是白痴。struts2果然很强,不管什么都会拦截,然后就会出现说action没有定义的错误,弄得要跳楼了的。原来想自定义拦截器,但是头晕晕的,也不知道行不行,就没弄。
后来几经波折,在网上找到了一篇提问的文章,也是说这个话题。原来没仔细看,篇幅很长,到后面才说问题解决了的。研究了下,果然可以,目前幸福中,哈哈。
- flex+struts2上传文件
- flex+struts2上传文件
- flex struts2单文件上传、同名文件上传、多文件上传
- flex 文件上传
- Flex + servlet 上传文件
- flex+php文件上传
- flex 多文件上传
- flex上传文件
- Flex文件上传
- Flex FTP文件上传
- flex +jsp 文件上传
- flex文件上传1
- flex多文件上传
- FLEX文件上传
- Flex文件上传
- Flex 文件上传
- Flex多文件上传
- Flex上传单个文件
- 内存泄露检测之: VC内存泄露检查工具--Visual Leak Detector
- 设计模式之适配器模式(Adapter)摘录
- OpenCV训练分类器
- Hadoop Version History and Feature
- elasticsearch 开始 —— 安装
- flex+struts2上传文件
- C++中的const 引用
- MySql在建立索引优化时需要注意的问题
- convertall release 6.0基于python: unit conversion
- python3.x安装setuptool出现openkey() argument 2 must...解决方法
- 专访许鹏:谈C程序员修养及大型项目源码阅读与学习
- 树的定义和基本概念
- detertdrt
- Log4j笔记