传送连接 : http://182.254.145.145/group1/M00/00/00/Co8plFWPh0LvBBVSAACAYF6Y1Z8204.htm
在用python测试上传的时候发现一旦文件名是汉字的程序就会爆出异常,无法将汉字的文件名 转码,然后在网上搜索(都差不多估计都是转的)大部分使用的方式是 relaod重载sys模块, 在用sys模块设置默认编码,但是python3里没有reload函数了。 1那么如果我们想用的话怎么办? 其实python3 内置的编码格式就是unicode ,我们只需要将 写好的代码保存成utf8的就可以了。 2可以查看一下环境变量,如果LANG=c 你需要在 /etc/profile 里把LANG= LANG=zh_CN.UTF-8 导出,就可以了,不需要在文件头写 # -*- coding:utf-8 -*- 这种东西了,他默认的就是。 服务器接文件 import tornado.web import os 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 这么多的坑,怎么填? |