flex 实现 文件上传下载
来源:互联网 发布:淘宝上淘口令怎么生成 编辑:程序博客网 时间:2024/05/01 22:09
java 后台 核心代码
上传:
package com.jiajie.service.m020400111;
import java.io.File;
import java.io.IOException;
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;
/**
* 学生照片批量上传servlet
* @author chengxuyuan
*
*/
@SuppressWarnings("serial")
public class FileUpload extends HttpServlet {
// 定义文件上传的路径
@SuppressWarnings("unused")
private String uploadPath = "";
private int maxPostSize = 100 * 1024 * 1024;
public FileUpload() {
super();
}
public void destroy() {
super.destroy();
}
protected void processRequest(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
String uploadPath= request.getSession().getServletContext().getRealPath("");
request.setCharacterEncoding("UTF-8");
uploadPath=uploadPath.replaceAll("\\\\", "/")+"/word/";
File f=new File(uploadPath);
if(!(f.exists())){
f.mkdirs();
}
response.setContentType("text/html;charset=UTF-8");
// 保存文件到服务器中
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();
try {
item.write(new File(uploadPath + name));
} catch (Exception e) {
e.printStackTrace();
}
}
}
} catch (FileUploadException e) {
e.printStackTrace();
}
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
}
下载 代码:
package com.jiajie.util;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URLDecoder;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class FileDownloadServlet extends HttpServlet {
private static final String CONTENT_TYPE = "text/html; charset=utf-8";
//Initialize global variables
public void init() throws ServletException {
}
//Process the HTTP Get request
public void doGet(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
// request.setCharacterEncoding("utf-8");
response.setContentType(CONTENT_TYPE);
String uploadPath= request.getSession().getServletContext().getRealPath("");
//得到下载文件的名字
//String filename=request.getParameter("filename");
//解决中文乱码问题
String filename=request.getParameter("filename");
filename = URLDecoder.decode(filename);
String path = request.getParameter("path");
// System.out.println("路径: "+uploadPath + "\\" + path +"\\"+filename);
//创建file对象
File file=new File(uploadPath + "\\" + path +"\\"+filename);
//设置response的编码方式
response.setContentType("application/x-msdownload");
//写明要下载的文件的大小
response.setContentLength((int)file.length());
//设置附加文件名
// response.setHeader("Content-Disposition","attachment;filename="+filename);
//解决中文乱码
response.setHeader("Content-Disposition","attachment;filename="+new String
(filename.getBytes("utf-8"),"iso-8859-1"));
//读出文件到i/o流
FileInputStream fis=new FileInputStream(file);
BufferedInputStream buff=new BufferedInputStream(fis);
byte [] b=new byte[1024];//相当于我们的缓存
long k=0;//该值用于计算当前实际下载了多少字节
//从response对象中得到输出流,准备下载
OutputStream myout=response.getOutputStream();
//开始循环下载
while(k<file.length()){
int j=buff.read(b,0,1024);
k+=j;
//将b中的数据写到客户端的内存
myout.write(b,0,j);
}
//将写入到客户端的内存的数据,刷新到磁盘
myout.flush();
}
//Process the HTTP Post request
public void doPost(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
doGet(request, response);
}
//Clean up resources
public void destroy() {
}
}
flex 前端 核心代码:
上传:
[Bindable]
private var fileRef:FileReferenceList = new FileReferenceList();
[Bindable]
private var selectedFiles:ArrayCollection = new ArrayCollection();//存放所有选择的文件
protected function button3_clickHandler(event:MouseEvent):void
{
// TODO Auto-generated method stub
//浏览按钮点击事件
fileRef.browse([new FileFilter("Documents", "*.pdf;*.doc;*.txt")]);
}
protected override function createChildren():void {
//覆盖父类的方法,加载页面时执行,初始化文件及添加监听事件
super.createChildren();
fileRef.addEventListener(Event.SELECT, file_select);
fileRef.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
}
[Bindable]
[Bindable]private var reqUrll:String = Sys.serverUrl + "/uplodSqBook";
//判断选择了哪些文件
private function file_select (e:Event):void {
for each (var f: FileReference in fileRef.fileList)
{
//首先判断文件是否已经存在
for(var i:int=0;i<selectedFiles.length;i++){
var f1: FileReference=selectedFiles.getItemAt(i) as FileReference;
if(f.name==f1.name){//但照片吧存在时才添加
selectedFiles.removeItemAt(0);
}
}
//判断选择的照片名是否合格
sqbook=f.name
selectedFiles.addItem(f);
var request:URLRequest = new URLRequest(reqUrll);
f.upload(request);
}
}
//出错时调用的方法
private function ioErrorHandler(e:IOErrorEvent): void
{
Pop.alert(e.text,"",4);
}
下载
private var downloadURL:URLRequest;
private var DownLoadfile:FileReference; //这是要主要的地方
//http://XX.XX.XX.XX:8080/upload/main.zip
private function downLoadFiles(urlAdd:String):void
{
downloadURL = new URLRequest(urlAdd);
DownLoadfile = new FileReference();
configureListeners(DownLoadfile);
DownLoadfile.download(downloadURL);
}
private function configureListeners(dispatcher:IEventDispatcher):void {
dispatcher.addEventListener(Event.COMPLETE, completeHandler);
}
private function completeHandler(event:Event):void {
Pop.alert("文件下载成功");
}
- flex 实现 文件上传下载
- Flex文件上传下载
- Flex上传下载的实现
- jspsmart实现文件上传下载
- jspsmart实现文件上传下载
- c#实现文件上传下载
- jspSmartUpload 实现文件上传下载
- BCB实现文件上传下载
- jspsmart实现文件上传下载
- jsp实现文件上传下载
- 文件流实现上传下载
- iOS-实现文件上传下载
- YII实现文件上传下载
- iOS-实现文件上传下载 .
- iOS 实现文件上传下载
- socket 实现文件上传下载
- GWT实现文件上传下载
- C#实现文件上传下载
- 如何让多行的EditText光标从最前头开始?
- Java中截取定长字符串的方法
- sendRedirect传递参数为null
- 最受欢迎的C++代码段
- myeclipse安装svn方法汇总
- flex 实现 文件上传下载
- mysql 创建用户后分配权限失败的解决方法
- elasticsearch更改node id生成方法
- POJ1979-Red and Black
- jQuery、Ajax分页
- linux硬盘更换扩容,lvm扩大usr与samba服务器
- 矩阵乘法模板之hdu——1751
- wifi破解不是真黑客不靠谱?
- Fibonacci(斐波那契)