加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

flex 实现 文件上传下载

发布时间:2020-12-15 03:40:48 所属栏目:百科 来源:网络整理
导读: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.HttpServle


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,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,1024);
??????????? k+=j;

??????????? //将b中的数据写到客户端的内存
??????????? myout.write(b,j);

??????? }

??????? //将写入到客户端的内存的数据,刷新到磁盘
??????? myout.flush();

??? }

??? //Process the HTTP Post request
??? public void doPost(HttpServletRequest request,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("文件下载成功"); ?? ??? ??? ?}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读