log4j学习 | hibernate3.3.x Failed to load class org. ...
2009-08-19
flex多文件上传
博客分类:
flex
FlexServletApacheAdobeXML
代码都是别人的
Xml代码
<?xml version="1.0" encoding="utf-8"?>
<mx:WindowedApplication xmlns:mx="http://www.adobe.com/2006/mxml"
layout="vertical" windowComplete="init()">
<mx:Script>
<![CDATA[
import mx.controls.Alert;
// private var fileList:FileReferenceList;
// private function init():void{
// fileList=new FileReferenceList();
// fileList.addEventListener(Event.SELECT,onSelect);
// fileList.addEventListener(Event.CANCEL,onCancel);
// }
[Bindable]
private var files:Array;
private var file:File;
private function init():void{
file=new File();
file.addEventListener(FileListEvent.SELECT_MULTIPLE,onSelectMuti);
}
private function onSelectMuti(event:FileListEvent):void{
this.files=event.files;
}
private function onSelect(event:Event):void{
}
private function onCancel(event:Event):void{
}
private function select():void{
file.browseForOpenMultiple("选择上传文件");
}
private function upLoad():void{
for(var i:int=0;i<this.files.length;i++){
var file:File=files[i];
file.addEventListener(Event.COMPLETE,onComplete);
file.upload(new URLRequest("http://localhost:8400/gdg/fileupLoad/FileUpLoad"));
}
}
private function onComplete(event:Event):void{
var file:File=event.target as File;
mx.controls.Alert.show(file.nativePath+"上传成功");
}
private function onIOError():void{
}
]]>
</mx:Script>
<mx:VBox width="100%">
<mx:HBox>
<mx:Button label="选择" click="select()"/>
<mx:Button label="上传" click="upLoad()"/>
</mx:HBox>
<mx:DataGrid dataProvider="{this.files}" width="100%">
<mx:columns>
<mx:DataGridColumn headerText="文件名">
</mx:DataGridColumn>
<mx:DataGridColumn headerText="图片">
<mx:itemRenderer>
<mx:Component>
<mx:Image source="{data.nativePath}" width="100" height="100">
</mx:Image>
</mx:Component>
</mx:itemRenderer>
</mx:DataGridColumn>
<mx:DataGridColumn headerText="上传进度">
<mx:itemRenderer>
FileUPLoad.test
</mx:itemRenderer>
</mx:DataGridColumn>
</mx:columns>
</mx:DataGrid>
</mx:VBox>
</mx:WindowedApplication>
<?xml version="1.0" encoding="utf-8"?>
<mx:WindowedApplication xmlns:mx="http://www.adobe.com/2006/mxml"
layout="vertical" windowComplete="init()">
<mx:Script>
<![CDATA[
import mx.controls.Alert;
// private var fileList:FileReferenceList;
// private function init():void{
// fileList=new FileReferenceList();
// fileList.addEventListener(Event.SELECT,onSelect);
// fileList.addEventListener(Event.CANCEL,onCancel);
// }
[Bindable]
private var files:Array;
private var file:File;
private function init():void{
file=new File();
file.addEventListener(FileListEvent.SELECT_MULTIPLE,onSelectMuti);
}
private function onSelectMuti(event:FileListEvent):void{
this.files=event.files;
}
private function onSelect(event:Event):void{
}
private function onCancel(event:Event):void{
}
private function select():void{
file.browseForOpenMultiple("选择上传文件");
}
private function upLoad():void{
for(var i:int=0;i<this.files.length;i++){
var file:File=files[i];
file.addEventListener(Event.COMPLETE,onComplete);
file.upload(new URLRequest("http://localhost:8400/gdg/fileupLoad/FileUpLoad"));
}
}
private function onComplete(event:Event):void{
var file:File=event.target as File;
mx.controls.Alert.show(file.nativePath+"上传成功");
}
private function onIOError():void{
}
]]>
</mx:Script>
<mx:VBox width="100%">
<mx:HBox>
<mx:Button label="选择" click="select()"/>
<mx:Button label="上传" click="upLoad()"/>
</mx:HBox>
<mx:DataGrid dataProvider="{this.files}" width="100%">
<mx:columns>
<mx:DataGridColumn headerText="文件名">
</mx:DataGridColumn>
<mx:DataGridColumn headerText="图片">
<mx:itemRenderer>
<mx:Component>
<mx:Image source="{data.nativePath}" width="100" height="100">
</mx:Image>
</mx:Component>
</mx:itemRenderer>
</mx:DataGridColumn>
<mx:DataGridColumn headerText="上传进度">
<mx:itemRenderer>
FileUPLoad.test
</mx:itemRenderer>
</mx:DataGridColumn>
</mx:columns>
</mx:DataGrid>
</mx:VBox>
</mx:WindowedApplication>
FileUPLoad.test
Xml代码
<?xml version="1.0" encoding="utf-8"?>
<mx:Canvas xmlns:mx="http://www.adobe.com/2006/mxml">
<mx:Script>
<![CDATA[
import mx.controls.ProgressBar;
private var _data:Object
override public function set data(value:Object):void{
this._data=value;
File(this._data).addEventListener(ProgressEvent.PROGRESS,onPro);
File(this._data).addEventListener(Event.COMPLETE,onCom);
}
override public function get data():Object{
return this._data;
}
private function onCom(event:Event):void{
hb.removeChild(bar);
lbProgress.text="上传成功";
}
private function onPro(e:ProgressEvent):void{
lbProgress.text = " 已上传 " + e.bytesLoaded
+ " 字节,共 " + e.bytesTotal + " 字节";
var proc: uint = e.bytesLoaded / e.bytesTotal * 100;
bar.setProgress(proc, 100);
bar.label= "当前进度: " + " " + proc + "%";
}
]]>
</mx:Script>
<mx:HBox id="hb">
<mx:ProgressBar minimum="0" maximum="100" id="bar">
</mx:ProgressBar>
<mx:Label id="lbProgress">
</mx:Label>
</mx:HBox>
</mx:Canvas>
<?xml version="1.0" encoding="utf-8"?>
<mx:Canvas xmlns:mx="http://www.adobe.com/2006/mxml">
<mx:Script>
<![CDATA[
import mx.controls.ProgressBar;
private var _data:Object
override public function set data(value:Object):void{
this._data=value;
File(this._data).addEventListener(ProgressEvent.PROGRESS,onPro);
File(this._data).addEventListener(Event.COMPLETE,onCom);
}
override public function get data():Object{
return this._data;
}
private function onCom(event:Event):void{
hb.removeChild(bar);
lbProgress.text="上传成功";
}
private function onPro(e:ProgressEvent):void{
lbProgress.text = " 已上传 " + e.bytesLoaded
+ " 字节,共 " + e.bytesTotal + " 字节";
var proc: uint = e.bytesLoaded / e.bytesTotal * 100;
bar.setProgress(proc, 100);
bar.label= "当前进度: " + " " + proc + "%";
}
]]>
</mx:Script>
<mx:HBox id="hb">
<mx:ProgressBar minimum="0" maximum="100" id="bar">
</mx:ProgressBar>
<mx:Label id="lbProgress">
</mx:Label>
</mx:HBox>
</mx:Canvas>
Java代码
package test;
import javax.servlet.http.HttpServlet;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Iterator;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
public class FileUpLoadServlet extends HttpServlet {
/**
*
*/
private static final long serialVersionUID = -1427340872248925684L;
/**
*
* @author crystal
*/
/**
* Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
* @param request servlet request
* @param response servlet response
*/
// 定义文件的上传路径
private String uploadPath = "d:\\a\\";
// 限制文件的上传大小
private int maxPostSize = 100 * 1024 * 1024;
public void destroy() {
super.destroy();
}
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
System.out.println("Access !");
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
//保存文件到服务器中
DiskFileItemFactory factory = new DiskFileItemFactory();
factory.setSizeThreshold(4096);
ServletFileUpload upload = new ServletFileUpload(factory);
upload.setSizeMax(maxPostSize);
try {
List fileItems = upload.parseRequest(request);
Iterator iter = fileItems.iterator();
while (iter.hasNext()) {
FileItem item = (FileItem) iter.next();
if (!item.isFormField()) {
String name = item.getName();
System.out.println(name);
try {
item.write(new File(uploadPath + name));
// SaveFile s = new SaveFile();
System.out.println("成功写入"+uploadPath+name);
// s.saveFile(name);
} catch (Exception e) {
e.printStackTrace();
}
}
}
} catch (FileUploadException e) {
e.printStackTrace();
System.out.println(e.getMessage() + "结束");
}
}
// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
/**
* Handles the HTTP <code>GET</code> method.
* @param request servlet request
* @param response servlet response
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
/**
* Handles the HTTP <code>POST</code> method.
* @param request servlet request
* @param response servlet response
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
/**
* Returns a short description of the servlet.
*/
public String getServletInfo() {
return "Short description";
}
// </editor-fold>
}