学习 tornado 服务器 - 配合fastdfs上传下载

 空间收藏于 2014-06-27 01:34 前端

 


传送链接 : http://182.254.145.145/group1/M00/00/00/Co8plFWPh0LvBBVSAACAYF6Y1Z8204.htm

在用python测试上传的时候发现一旦文件名是汉字的程序就会爆出异常,没法将汉字的文件名

转码,而后在网上搜索(都差很少估计都是转的)大部分使用的方式是 relaod重载sys模块,python

在用sys模块设置默认编码,可是python3里没有reload函数了。web

 

1那么若是咱们想用的话怎么办? 其实python3 内置的编码格式就是unicode ,咱们只须要将json

写好的代码保存成utf8的就能够了。服务器

 

2能够查看一下环境变量,若是LANG=c 你须要在 /etc/profile 里把LANG= LANG=zh_CN.UTF-8app

导出,就能够了,不须要在文件头写 # -*- coding:utf-8 -*- 这种东西了,他默认的就是。webapp

 

服务器接文件
 
函数

import tornado.webtornado

import ospost

import json

class UploadFileHandler(tornado.web.RequestHandler):

 

def get( self ) :

pass;

 

def post ( self ):

try :

upload_path=os.path.join(os.path.dirname("/data/www/xx"),'temp');

file_metas=self.request.files['file'];

 

for meta in file_metas:

filename=meta['filename']

filepath=os.path.join(upload_path,filename)

with open(filepath,'wb') as up:

up.write(meta['body']);

client_file='/etc/fdfs/client.conf'

cmdline='fdfs_upload_file %s %s' % ("/etc/fdfs/client.conf",filepath);

retcode=os.popen(cmdline).readlines();

result={};

result['result']=0;

result['file']=retcode[0];

self.write(json.dumps(result))

except Exception :

result={};

result['result']=1;

result['error']=e;

self.write(json.dumps(result));
 

 

客户端上传

package com.webapp.upload

{

import flash.display.InteractiveObject;

import flash.events.DataEvent;

import flash.events.Event;

import flash.events.MouseEvent;

import flash.events.ProgressEvent;

import flash.net.FileReference;

import flash.net.URLRequest;

 

public class UploadFileHandler

{

protected var clickItor:InteractiveObject;

protected var request:URLRequest ;

public function UploadFileHandler(itor:InteractiveObject,url)

{

clickItor = itor;

request = url;

clickItor.addEventListener(MouseEvent.CLICK , onChooseFile);

}

protected var file:FileReference ;

protected function onChooseFile(e:MouseEvent):void

{

file = new FileReference();

file.browse();

file.addEventListener(Event.SELECT, onSelectFile);

}

protected var fileName:String = "";

protected function onSelectFile(e:Event):void

{

fileName = (file.name);

file.addEventListener(Event.COMPLETE , onLoadedInRam);

file.load();

}

protected function onLoadedInRam(e:Event):void

{

trace("start ...upload");

file.removeEventListener(Event.COMPLETE , onLoadedInRam);

trace(file.data.length);

file.upload(request,"file");

file.addEventListener(Event.COMPLETE , onLoadedInRequest);

file.addEventListener(DataEvent.UPLOAD_COMPLETE_DATA, onResponeData);

file.addEventListener(ProgressEvent.PROGRESS, onProcessHandler);

}

protected function onProcessHandler(e:ProgressEvent):void

{

trace(int(e.bytesLoaded/e.bytesTotal * 100) + "%");

}

protected function onResponeData(e:DataEvent):void

{

trace(e.data);

}

protected function onLoadedInRequest(e:Event):void

{

trace("is done");

}

}

}



输出结果
start ...upload
19716
100%
is done
{"result": 0, "file": "group1/M00/00/00/Co8plFWPgLTHsaTpAABNBD70KHg613.xml\n"}
 
 

这样的话就支持全部的编码了,那么下一步咱们须要将上传到临时目录

的文件上载到fastdfs里, 而后返回fast的地址给前端,大体的思路就是

如此。固然这中间的业务逻辑就不说了,咱们只讨论的是上传和下载。

 

另外不得不吐槽一下, python也是够冷门的, 查了那么多资料居然

都python2x的,python3的解决方案太少,大部分都是转的,python

这么多的坑,怎么填? 

相关文章
相关标签/搜索