jfinal文件上傳路徑
❶ JFinal 如何將操作日誌存入到資料庫中
操作日誌,也分粗細顆粒.
比如常見的 配置JFinal的Handler,配置LogHandler的處理器,Handler可以接管所有web請求,這里可以做粗顆粒的處理,對每一個請求做入庫處理,如果訪問量大時,入庫操作做列隊處理就可以了.
細顆粒的,如在java代碼中的Loglog = Log.getLog("JAVA類描述或類名");
這個一般做入庫的還是少,畢竟已經有很成熟的日誌分析查看工具了,可以直接查文件了.
如果業務需要做這個,那也很簡單:
1:需要實現一個介面ILogFactory :
/**
*JdkLogFactory.
*/
{
publicLoggetLog(Class<?>clazz){
returnnewDbLog(clazz);
}
publicLoggetLog(Stringname){
returnnewDbLog(name);
}
}
2: DbLog參考jfinal代碼中 com.jfinal.log.Log4jLog 把這個類復制一下,在每個方法中增加一個入庫的操作,當然量大時,入庫操作做列隊處理就可以了.
3:在JFinalConfig中:
@Override
publicvoidconfigConstant(Constantsme){
//先啟動資料庫
ActiveRecordPlugin可以獨立於javaweb環境運行在任何普通的java程序中,使用方式極度簡單,相對於web項目只需要手動調用一下其start()方法即可立即使用。
//設置為你的工廠DbLogFactory
me.setLogFactory(newDbLogFactory());
}
❷ jfinal如何實現文件上傳到非工程目錄下並可
1 , 設置全局
@Override
public void configConstant(Constants me) {
me.setBaseUploadPath(String baseUploadPath)
}
/**
*Setfilebaseuploadpath.
*設置文件上傳保存基礎路徑,當路徑以"/"打頭或是以windows磁碟盤符打頭,
*則將路徑設置為絕對路徑,否則路徑將是以應用根路徑為基礎的相對路徑
*<pre>
*例如:
*1:參數"/var/www/upload"為絕對路徑,上傳文件將保存到此路徑之下
*2:參數"upload"為相對路徑,上傳文件將保存到PathKit.getWebRoot()+"/upload"路徑之下
*</pre>
*/
publicvoidsetBaseUploadPath(StringbaseUploadPath){
if(StrKit.isBlank(baseUploadPath)){
("baseUploadPathcannotbeblank.");
}
this.baseUploadPath=baseUploadPath;
}
2 , 使用Java的文件拷貝或者移動
UploadFile.getFile().renameTo(newFile);
❸ jfinal上傳怎樣改變上傳文件的名稱
在firefox上,input(type=file)默認獲取到的value值就是文件名。
在IE上,input(type=file)默認獲取到的value值是路徑。
因此用js來獲取fileupload中的上傳文件的文件名需要多方面考慮。
❹ jfinal怎麼上傳文件
JFinal約定,如果保存路徑是以 "/" 或者 "X:/" 打頭使用的是絕對路徑,否則使用相對路徑,默認的相對路徑為 WebRoot/upload,這個相對路徑還可以通過 me.setUploadedFileSaveDirectory(...)來設置
❺ jfinal 如何導出zip壓縮文件
官網介紹:JFinal 是基於 Java 語言的極速 WEB + ORM 框架,其核心設計目標是開發迅速、代碼量少、學習簡單、功能強大、輕量級、易擴展、Restful。在擁有Java語言所有優勢的同時再擁有ruby、python、php等動態語言的開發效率!為您節約更多時間,去陪戀人、家人和朋友 :)
Jfinal做為後台,進行下載文件服務時,源碼中可看到:
Controller中已經提供了,方法:
/**
*Renderwithfile
*/
publicvoidrenderFile(StringfileName){
render=renderManager.getRenderFactory().getFileRender(fileName);
}
/**
*Renderwithfile,
*/
publicvoidrenderFile(StringfileName,StringdownloadFileName){
render=renderManager.getRenderFactory().getFileRender(fileName,downloadFileName);
}
/**
*Renderwithfile
*/
publicvoidrenderFile(Filefile){
render=renderManager.getRenderFactory().getFileRender(file);
}
/**
*Renderwithfile,
file=文件,downloadFileName=下載時客戶端顯示的文件名稱,很貼心
*/
publicvoidrenderFile(Filefile,StringdownloadFileName){
render=renderManager.getRenderFactory().getFileRender(file,downloadFileName);
}
大家可以看到源碼中 FileRender 是有處理各個瀏覽器的兼容問題,所以可以方便的使用
/**
*Copyright(c)2011-2017,JamesZhan詹波([email protected]).
*
*LicensendertheApacheLicense,Version2.0(the"License");
*.
*YoumayobtainaoftheLicenseat
*
*http://www.apache.org/licenses/LICENSE-2.0
*
*,software
*"ASIS"BASIS,
*,eitherexpressorimplied.
*
*limitationsundertheLicense.
*/
packagecom.jfinal.render;
importjava.io.BufferedInputStream;
importjava.io.File;
importjava.io.FileInputStream;
importjava.io.IOException;
importjava.io.InputStream;
importjava.io.OutputStream;
importjava.io.UnsupportedEncodingException;
importjava.net.URLEncoder;
importjavax.servlet.ServletContext;
importjavax.servlet.http.HttpServletRequest;
importjavax.servlet.http.HttpServletResponse;
importcom.jfinal.kit.LogKit;
importcom.jfinal.kit.StrKit;
/**
*FileRender.
*/
{
_CONTENT_TYPE="application/octet-stream";
;
;
protectedFilefile;
=null;
publicFileRender(Filefile){
if(file==null){
("filecannotbenull.");
}
this.file=file;
}
publicFileRender(Filefile,StringdownloadFileName){
this(file);
if(StrKit.isBlank(downloadFileName)){
("downloadFileNamecannotbeblank.");
}
this.downloadFileName=downloadFileName;
}
publicFileRender(StringfileName){
if(StrKit.isBlank(fileName)){
("fileNamecannotbeblank.");
}
StringfullFileName;
fileName=fileName.trim();
if(fileName.startsWith("/")||fileName.startsWith("\")){
if(baseDownloadPath.equals("/")){
fullFileName=fileName;
}else{
fullFileName=baseDownloadPath+fileName;
}
}else{
fullFileName=baseDownloadPath+File.separator+fileName;
}
this.file=newFile(fullFileName);
}
publicFileRender(StringfileName,StringdownloadFileName){
this(fileName);
if(StrKit.isBlank(downloadFileName)){
("downloadFileNamecannotbeblank.");
}
this.downloadFileName=downloadFileName;
}
staticvoidinit(StringbaseDownloadPath,ServletContextservletContext){
FileRender.baseDownloadPath=baseDownloadPath;
FileRender.servletContext=servletContext;
}
publicvoidrender(){
if(file==null||!file.isFile()){
RenderManager.me().getRenderFactory().getErrorRender(404).setContext(request,response).render();
return;
}
//---------
response.setHeader("Accept-Ranges","bytes");
Stringfn=downloadFileName==null?file.getName():downloadFileName;
response.setHeader("Content-disposition","attachment;"+encodeFileName(request,fn));
StringcontentType=servletContext.getMimeType(file.getName());
response.setContentType(contentType!=null?contentType:DEFAULT_CONTENT_TYPE);
//---------
if(StrKit.isBlank(request.getHeader("Range"))){
normalRender();
}else{
rangeRender();
}
}
protectedStringencodeFileName(StringfileName){
try{
//returnnewString(fileName.getBytes("GBK"),"ISO8859-1");
returnnewString(fileName.getBytes(getEncoding()),"ISO8859-1");
}catch(UnsupportedEncodingExceptione){
returnfileName;
}
}
/**
*依據瀏覽器判斷編碼規則
*/
publicStringencodeFileName(HttpServletRequestrequest,StringfileName){
StringuserAgent=request.getHeader("User-Agent");
try{
StringencodedFileName=URLEncoder.encode(fileName,"UTF8");
//如果沒有UA,則默認使用IE的方式進行編碼
if(userAgent==null){
return"filename=""+encodedFileName+""";
}
userAgent=userAgent.toLowerCase();
//IE瀏覽器,只能採用URLEncoder編碼
if(userAgent.indexOf("msie")!=-1){
return"filename=""+encodedFileName+""";
}
//Opera瀏覽器只能採用filename*
if(userAgent.indexOf("opera")!=-1){
return"filename*=UTF-8''"+encodedFileName;
}
//Safari瀏覽器,只能採用ISO編碼的中文輸出,Chrome瀏覽器,只能採用MimeUtility編碼或ISO編碼的中文輸出
if(userAgent.indexOf("safari")!=-1||userAgent.indexOf("applewebkit")!=-1||userAgent.indexOf("chrome")!=-1){
return"filename=""+newString(fileName.getBytes("UTF-8"),"ISO8859-1")+""";
}
//FireFox瀏覽器,可以使用MimeUtility或filename*或ISO編碼的中文輸出
if(userAgent.indexOf("mozilla")!=-1){
return"filename*=UTF-8''"+encodedFileName;
}
return"filename=""+encodedFileName+""";
}catch(UnsupportedEncodingExceptione){
thrownewRuntimeException(e);
}
}
protectedvoidnormalRender(){
response.setHeader("Content-Length",String.valueOf(file.length()));
InputStreaminputStream=null;
OutputStreamoutputStream=null;
try{
inputStream=newBufferedInputStream(newFileInputStream(file));
outputStream=response.getOutputStream();
byte[]buffer=newbyte[1024];
for(intlen=-1;(len=inputStream.read(buffer))!=-1;){
outputStream.write(buffer,0,len);
}
outputStream.flush();
outputStream.close();
}catch(IOExceptione){
Stringn=e.getClass().getSimpleName();
if(n.equals("ClientAbortException")||n.equals("EofException")){
}else{
thrownewRenderException(e);
}
}catch(Exceptione){
thrownewRenderException(e);
}finally{
if(inputStream!=null)
try{inputStream.close();}catch(IOExceptione){LogKit.error(e.getMessage(),e);}
}
}
protectedvoidrangeRender(){
Long[]range={null,null};
processRange(range);
StringcontentLength=String.valueOf(range[1].longValue()-range[0].longValue()+1);
response.setHeader("Content-Length",contentLength);
response.setStatus(HttpServletResponse.SC_PARTIAL_CONTENT);//status=206
//Content-Range:bytes0-499/10000
StringBuildercontentRange=newStringBuilder("bytes").append(String.valueOf(range[0])).append("-").append(String.valueOf(range[1])).append("/").append(String.valueOf(file.length()));
response.setHeader("Content-Range",contentRange.toString());
InputStreaminputStream=null;
OutputStreamoutputStream=null;
try{
longstart=range[0];
longend=range[1];
inputStream=newBufferedInputStream(newFileInputStream(file));
if(inputStream.skip(start)!=start)
thrownewRuntimeException("Fileskiperror");
outputStream=response.getOutputStream();
byte[]buffer=newbyte[1024];
longposition=start;
for(intlen;position<=end&&(len=inputStream.read(buffer))!=-1;){
if(position+len<=end){
outputStream.write(buffer,0,len);
position+=len;
}
else{
for(inti=0;i<len&&position<=end;i++){
outputStream.write(buffer[i]);
position++;
}
}
}
outputStream.flush();
outputStream.close();
}
catch(IOExceptione){
Stringn=e.getClass().getSimpleName();
if(n.equals("ClientAbortException")||n.equals("EofException")){
}else{
thrownewRenderException(e);
}
}
catch(Exceptione){
thrownewRenderException(e);
}
finally{
if(inputStream!=null)
try{inputStream.close();}catch(IOExceptione){LogKit.error(e.getMessage(),e);}
}
}
/**
*Examplesofbyte-ranges-specifiervalues(assuminganentity-bodyoflength10000):
*Thefirst500bytes(byteoffsets0-499,inclusive):bytes=0-499
*Thesecond500bytes(byteoffsets500-999,inclusive):bytes=500-999
*Thefinal500bytes(byteoffsets9500-9999,inclusive):bytes=-500
*Orbytes=9500-
*/
protectedvoidprocessRange(Long[]range){
StringrangeStr=request.getHeader("Range");
intindex=rangeStr.indexOf(',');
if(index!=-1)
rangeStr=rangeStr.substring(0,index);
rangeStr=rangeStr.replace("bytes=","");
String[]arr=rangeStr.split("-",2);
if(arr.length<2)
thrownewRuntimeException("Rangeerror");
longfileLength=file.length();
for(inti=0;i<range.length;i++){
if(StrKit.notBlank(arr[i])){
range[i]=Long.parseLong(arr[i].trim());
if(range[i]>=fileLength)
range[i]=fileLength-1;
}
}
//Rangeformatlike:9500-
if(range[0]!=null&&range[1]==null){
range[1]=fileLength-1;
}
//Rangeformatlike:-500
elseif(range[0]==null&&range[1]!=null){
range[0]=fileLength-range[1];
range[1]=fileLength-1;
}
//checkfinalrange
if(range[0]==null||range[1]==null||range[0].longValue()>range[1].longValue())
thrownewRuntimeException("Rangeerror");
}
}