I wrote a small upload example.
Download jfinal-1.8_demo_for_jsp.zip from jfinal.
Download jfinal-1.8-lib.zip later
Delete the deleted items as required. Introduce some packages and the following project structure:
In democonfig. Java, only the root path is left:
/*** Configure route */Public void configroute (routes me) {me. add ("/", commoncontroller. class); // me. add ("/blog", blogcontroller. class );}
Commoncontroller. Java:
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:
<%@ 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">
Fileservice. Java:
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 (); // get the corresponding file channel out = fo. getchannel (); // get the corresponding file channel in. transferto (0, in. size (), OUT); // connect two channels, read from the in channel, and write to the out channel} catch (ioexception e) {e. printstacktrace ();} finally {try {fi. close (); In. close (); FO. close (); out. close ();} catch (ioexception e) {e. printstacktrace ();}}}}
There is not much to explain. Please refer to the official documents.