寫了個上傳的小例子。html
從jfinal官網下載jfinal-1.8_demo_for_jsp.zipjava
而後下載jfinal-1.8-lib.zipdom
按要求刪掉該刪除的,引入一些包,以後的項目結構:jsp
DemoConfig.java中配置路由,只留下了根路徑:post
/** * 配置路由 */ public void configRoute(Routes me) { me.add("/", CommonController.class); //me.add("/blog", BlogController.class); }
CommonController.java :ui
package com.demo.common; import java.io.File; import java.io.IOException; import java.util.UUID; import com.jfinal.core.Controller; import com.jfinal.upload.UploadFile; /** * CommonController */ public class CommonController extends Controller { public void index() { render("/index.jsp"); } public void uploadFile(){ UploadFile uploadFile=this.getFile(); String fileName=uploadFile.getOriginalFileName(); File file=uploadFile.getFile(); FileService fs=new FileService(); File t=new File("S:\\file\\"+UUID.randomUUID().toString()); try { t.createNewFile(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } fs.fileChannelCopy(file, t); file.delete(); this.renderHtml("success,<a href=\"./\">back</a>"); } }
index.jsp:this
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xml:lang="zh-CN" xmlns="http://www.w3.org/1999/xhtml" lang="zh-CN"> <head> <meta http-equiv="content-type" content="text/html; charset=UTF-8" /> </head> <body> <form action="uploadFile" enctype="multipart/form-data" method="post"> <input type="file" name="file"/> <input type="submit"/> </form> </body> </html>
FileService.java :spa
package com.demo.common; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.nio.channels.FileChannel; public class FileService { public void fileChannelCopy(File s, File t) { FileInputStream fi = null; FileOutputStream fo = null; FileChannel in = null; FileChannel out = null; try { fi = new FileInputStream(s); fo = new FileOutputStream(t); in = fi.getChannel();// 獲得對應的文件通道 out = fo.getChannel();// 獲得對應的文件通道 in.transferTo(0, in.size(), out);// 鏈接兩個通道,而且從in通道讀取,而後寫入out通道 } catch (IOException e) { e.printStackTrace(); } finally { try { fi.close(); in.close(); fo.close(); out.close(); } catch (IOException e) { e.printStackTrace(); } } } }
沒有太多須要說明的,參考着官方的文檔就能夠了。code