A simple example of file upload in jfinal

Source: Internet
Author: User

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.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.