How to Develop a java open-source framework ----- how to upload files and how to package files through the Jvn framework (Lecture 6); lecture 6 on strength of Party A; and lecture 6 on jvn

Source: Internet
Author: User

How to Develop a java open-source framework ----- how to upload files and how to package files through the Jvn framework (Lecture 6); lecture 6 on strength of Party A; and lecture 6 on jvn
Preface

I,

The blogger is teaching you how to develop a javaEE framework (Jvn framework). The blog has a complete development video. Every blog post is a knowledge point to help you understand the framework:

Blog home: http://www.cnblogs.com/everxs/

This content video and source code: http://pan.baidu.com/s/1pJsoGDd

1. Why should we use the java framework and what benefits can the framework bring. 2. helps you understand the principles of the Framework.

3. How is the framework implemented. 4. How to develop a java framework of your own.

II,
Currently, the developed framework is called the Jvn framework, and the author's name is "Gossip". Let's get started with the development framework.
The Jvn framework has the following features:
1. the mvc Framework, similar to the web framework StringMvc or struts, does not write servlets and does not need to write servlet configurations in web. xml.

2. The orm framework, similar to hibernate or ibatis, is not worrying about complicated jdbc operations.

3. The spring framework, similar to the spring framework, has never worried about bean management;

4. cache mechanism.

5. Regular scheduling.

6. automatically generate online documents.

7. interceptor

III,
Each blog post has a detailed explanation of the video and the corresponding source code, and the video is continuous. To learn how to develop the Jvn framework, we recommend that you start with the first blog post Jvn.

Content of this blog

Recall: In the previous lesson, we talked about how to customize Annotation and implement interceptor.

In this lesson, we will implement:

1. File Upload encapsulation: encapsulation of common parameters in the uploaded file.

2. encapsulation of common methods

Implementation ideas:

1, pilot into the commons-fileupload-1.3.1.jar, commons-io-2.4.jar these two packages.

2. Then, compile the getFile () and getFiles () methods in JvnController.

3. Upload the file to the Save directory and save the file name.

4. Put common parameters in the form into paramMap.

5. getFormParam () is used to obtain the common parameters for object uploading.

 

The following is the content video and the source code address:

 

Http://pan.baidu.com/s/1pJsoGDd

 

Ps: videos and code will be updated continuously. I hope you will like it and support it a lot.

QQ: 245223343 forever brother eight

 

The code I wrote is attached below:

Remember to first import the Jar package: commons-fileupload-1.3.1.jar, commons-io-2.4.jar. My source code can be directly downloaded.

The following is the JvnController class code:

Package com. jvn. core; import java. io. file; import java. io. IOException; import java. text. parseException; import java. text. simpleDateFormat; import java. util. arrayList; import java. util. date; import java. util. hashMap; import java. util. iterator; import java. util. list; import java. util. map; import javax. servlet. http. httpServletRequest; import javax. servlet. http. httpServletResponse; import org. apache. commons. fileupl Oad. fileItem; import org. apache. commons. fileupload. disk. diskFileItemFactory; import org. apache. commons. fileupload. servlet. servletFileUpload; import com. jvn. kit. fileKit; import com. jvn. kit. mapKit; import com. jvn. kit. strKit;/*** controller parent class * @ author Administrator **/public class JvnController {/*** inject two attributes when creating the Controller object */private HttpServletRequest request; private HttpServletResponse response; pri Vate SimpleDateFormat SDF_DATE_TIME = new SimpleDateFormat ("yyyy-MM-dd HH: mm: ss"); private SimpleDateFormat SDF_DATE = new SimpleDateFormat ("yyyy-MM-dd "); private Map <String, String> paramMap = new HashMap <String, String> (); public HttpServletRequest getRequest () {return request;} public void setRequest (HttpServletRequest request) {this. request = request;} public HttpServletResponse getResponse () {retur N response;} public void setResponse (HttpServletResponse response) {this. response = response ;} /*** the following are some input methods * // *** convert int * @ param paramName * @ param defaultValue * @ return */public int getInt (String paramName, int defaultValue) {int result = defaultValue; try {result = Integer. parseInt (getParam (paramName);} catch (Exception e) {} return result;}/*** convert int * @ param paramName * @ return */public int getI Nt (String paramName) {int result = Integer. parseInt (getParam (paramName); return result;}/*** convert double * @ param paramName * @ param defaultValue * @ return */public double getDouble (String paramName, double defaultValue) {double result = defaultValue; try {result = Double. parseDouble (getParam (paramName);} catch (Exception e) {} return result;}/*** convert to double * @ param paramName * @ param defaultValue * @ return */Public double getDouble (String paramName) {double result = Double. parseDouble (getParam (paramName); return result;}/*** convert float * @ param paramName * @ param defaultValue * @ return */public float getFloat (String paramName, float defaultValue) {float result = defaultValue; try {result = Float. parseFloat (getParam (paramName);} catch (Exception e) {} return result;}/*** convert float * @ param paramName * @ param d EfaultValue * @ return */public float getFloat (String paramName) {float result = Float. parseFloat (getParam (paramName); return result;}/*** convert boolen * @ param paramName * @ param defaultValue * @ return */public boolean getBoolean (String paramName) {boolean result = Boolean. parseBoolean (getParam (paramName); return result;}/*** convert boolean * @ param paramName * @ param defaultValue * @ return */public boolea N getBoolean (String paramName, boolean defaultValue) {boolean result = defaultValue; try {result = Boolean. parseBoolean (getParam (paramName);} catch (Exception e) {} return result ;} /*** convert date * @ param paramName * @ param defaultValue * @ return */public Date getDate (String paramName, Date defaultValue) {Date date = defaultValue; try {date = SDF_DATE.parse (getParam (paramName);} catch (Exception e) {} return date ;}/*** Convert to date * @ param paramName * @ param defaultValue * @ return */public Date getDate (String paramName) {Date date = null; try {date = SDF_DATE.parse (getParam (paramName);} catch (ParseException e) {e. printStackTrace (); throw new RuntimeException (e);} return date ;} /*** convert date * @ param paramName * @ param defaultValue * @ return */public Date getDateTime (String paramName, Date defaultValue) {Date date = DefaultValue; try {date = SDF_DATE_TIME.parse (getParam (paramName);} catch (Exception e) {} return date ;} /*** convert to date * @ param paramName * @ param defaultValue * @ return */public Date getDateTime (String paramName) {Date date = null; try {date = SDF_DATE_TIME.parse (getParam (paramName);} catch (ParseException e) {e. printStackTrace (); throw new RuntimeException (e);} return date;} public String getParam (String ParamName) {return request. getParameter (paramName);} public String getParam (String paramName, String defaultValue) {if (request. getParameter (paramName) = null) {return defaultValue;} else {return request. getParameter (paramName) ;}}/*** retrieve the request's paramMap and convert it to the map of key --> value [0] * @ return */public Map <String, String> getMap () {Map <String, String []> map = request. getParameterMap (); Map <String, String> _ map = new Ha ShMap <String, String> (); for (String s: map. keySet () {String [] values = map. get (s); _ map. put (s, values [0]);} return _ map;}/*** upload the Object to clazz * @ param clazz * @ return */public Object getObject (Class clazz) {Object = MapKit. mapToObject (getMap (), clazz); return object;}/*** returns String * @ param content */public void renderString (String content) {try {response. getWriter (). write (content);} catch (IOExcep Tion e) {e. printStackTrace () ;}}/*** returns a page */public void render (String view) {try {getRequest (). getRequestDispatcher (view ). forward (getRequest (), getResponse ();} catch (Exception e) {e. printStackTrace (); throw new RuntimeException (e) ;}/ *** redirect to a Url * @ param actionName */public void redirect (String actionName) {try {getResponse (). sendRedirect (actionName);} catch (IOException e) {// TODO Auto-genera Ted catch blocke. printStackTrace ();}/*** get the uploaded file, here you need to add a commons-fileupload.jar, commons-io.jar * @ param dirPath * @ param fileName * @ param maxSize 1024*1024*1 M * @ return */public void getFile (String dirPath, String fileNamePrefix, int maxSize) {try {DiskFileItemFactory diskFactory = new DiskFileItemFactory (); // threshold limit and critical value, that is, the hard disk cache is 1 M diskFactory. setSizeThreshold (40*1024*1024); String temPat H = request. getRealPath ("/tmpFile"); File dir = new File (temPath); if (! Dir. exists () {dir. mkdirs ();} System. out. println ("Temporary Folder path" + temPath); // repository storage room, that is, the temporary file directory diskFactory. setRepository (new File (temPath); ServletFileUpload upload = new ServletFileUpload (diskFactory); upload. setHeaderEncoding (JvnConfig. CONSTANT. getEncoding (); // set the maximum file size that can be uploaded to 4 MB 4*1024*1024 upload. setSizeMax (maxSize); // parse the HTTP request message header List fileItems = upload. parseRequest (request); Iterator I Ter = fileItems. iterator (); List <Object> list = new ArrayList <Object> (); while (iter. hasNext () {FileItem item = (FileItem) iter. next (); if (item. isFormField () {// Form Content String name = item. getFieldName (). trim (); String value = item. getString (JvnConfig. CONSTANT. getEncoding (); paramMap. put (name, value);} else {// file content String type = StrKit. getEndType (item. getName (),"\\. "); FileKit. saveFile (dirPath + fileN AmePrefix + ". "+ type, item. getInputStream ();}/*** Save the data to the database */} catch (Exception e) {e. getStackTrace (); throw new RuntimeException (e) ;}/ *** get the uploaded file, which requires an additional commons-fileupload.jar, commons-io.jar * @ param dirPath * @ param fileName * @ param maxSize 1024*1024*1 M * @ return */public void getFiles (String dirPath, String [] fileNamePrefix, int maxSize) {try {DiskFileItemFactory diskFactory = new DiskFile ItemFactory (); // threshold limit, critical value, that is, hard disk cache 1 M diskFactory. setSizeThreshold (40*1024*1024); String temPath = request. getRealPath ("/tmpFile"); File dir = new File (temPath); if (! Dir. exists () {dir. mkdirs ();} System. out. println ("Temporary Folder path" + temPath); // repository storage room, that is, the temporary file directory diskFactory. setRepository (new File (temPath); ServletFileUpload upload = new ServletFileUpload (diskFactory); upload. setHeaderEncoding (JvnConfig. CONSTANT. getEncoding (); // set the maximum file size that can be uploaded to 4 MB 4*1024*1024 upload. setSizeMax (maxSize); // parse the HTTP request message header List fileItems = upload. parseRequest (request); Iterator iter = fileItems. iterator (); List <Object> list = new ArrayList <Object> (); int count = 0; while (iter. hasNext () {FileItem item = (FileItem) iter. next (); if (item. isFormField () {// Form Content String name = item. getFieldName (). trim (); String value = item. getString (JvnConfig. CONSTANT. getEncoding (); paramMap. put (name, value);} else {// file content String type = StrKit. getEndType (item. getName (),"\\. "); if (count> (fileNamePrefix. length-1) {count = 0;} FileKit. saveFile (dirPath + fileNamePrefix [count] + ". "+ type, item. getInputStream (); count ++;}/*** Save the data to the database */} catch (Exception e) {e. getStackTrace (); throw new RuntimeException (e) ;}/ *** get the uploaded file, common form parameters * @ param attr * @ return */public String getFormParm (String attr) {return paramMap. get (attr );}}

 

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.