SSH Summary (II)

Source: Internet
Author: User

1. File Operations, read and write, and solve the garbled Problem

Read files

InputStreamReader isr = new InputStreamReader(new FileInputStream(new File(path)), "UTF-8");BufferedReader reader = new BufferedReader(isr);String s;while ((s = reader.readLine()) != null) {content += s + "\n";}reader.close();

Write files

Writer writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(newFile1.getAbsolutePath().toString()), "UTF-8"));writer.write(content);writer.close();

2. Common struts2 labels

Ticket: <s: Radio name = "gender" list = "# {'male': 'male', 'female ': female} "listkey =" key "listvalue =" value "value =" 'male' "/>

Time Format: <s: Date name = "publishtime" format = "YYYY mm dd hh: mm: SS"/>

Drop-down box: <s: select list = "# request. role "name =" role1 "value =" rolename "Key =" ID "headerkey =" 0 "headervalue =" select role "> </S: Select>

3. upload files through Ajax

Ajax uses the ajaxfileupload. js plug-in to upload files. The Ajax code is as follows:

HTML code:

<Input id = "filetoupload" type = "file" size = "20" name = "myfile" class = "input"> <button type = "button" id = "buttonupload" data-dismiss = "Modal" class = "BTN-primary"> upload </button>

JS Code:

 

$ ("# Buttonupload "). click (function () {// verify the image format var format = $ ("# filetoupload "). val (); var type = format. substring (format. lastindexof (". ") + 1, format. length ). tolowercase (); onsole.info (format); console.info (type); If (type! = "Jpg" & type! = "Jpeg" & type! = "BMP" & type! = "GIF" & type! = "PNG") {alert ("please upload the correct image format"); Return ;}$. ajaxfileupload ({URL: 'notice _ addimage. action ', // processing image script ecureuri: false, fileelementid: 'filetupload', // file control iddatatype: 'text', success: function (data) {$ ("# dd" ).html (data); VaR value = $ ("# dd pre" ).html (); if (value = "undefined" | value = NULL) {value = data;} console.info ("DD:" + value); $ ("# btn_image "). val (value); console.info ("Image:" + $ ("# btn_image "). val (); $ ("# tooltip" ).html ("image imported successfully") ;}, error: function (data) {$ ("# dd "). val (data); console.info ("error"); alert ("error ");}});});

  

Java code:

 

1 // The myfile attribute is used to encapsulate the uploaded file 2 private file myfile; 3 4 // The myfilecontenttype attribute is used to encapsulate the type of the uploaded file 5 private string myfilecontenttype; 6 7 // The myfilefilename attribute is used to encapsulate the object name to be uploaded. The object name is 8 private string myfilefilename; 9 inputstream is; 10 try {11 is = new fileinputstream (myfile ); 12 // set the Upload File directory 13 string uploadpath = templateutils. basepath + "\ upload"; 14 // rename the file 15 string filename = stringutils. getuuid () + this. getmyfilefilename (). substring (myfilefilename. lastindexof (". "), myfilefilename. length (); 16 // set the target file 17 file tofile = new file (uploadpath, filename); 18 // create an output stream 19 outputstream OS = new fileoutputstream (tofile ); 20 // set cache 21 byte [] buffer = new byte [1024]; 22 int length = 0; 23 // read the myfile file and output it to the tofile 24 while (length = is. read (buffer)> 0) {25 OS. write (buffer, 0, length); 26} 27 // close the input stream 28 is. close (); 29 // close the output stream for 30 OS. close ();

 

  

 

 

  

 

  

SSH Summary (II)

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.