SSH總結(二)

來源:互聯網
上載者:User

標籤:style   blog   color   java   os   使用   io   檔案   for   

1、檔案的操作,讀寫檔案,解決亂碼問題

讀檔案

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();

 寫檔案

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

 2、struts2常用標籤

單選框:<s:radio name="Gender" list="#{‘男‘:‘男‘,‘女‘:‘女‘}" listKey="key" listValue="value" value="‘男‘" />

時間格式化:<s:date name="publishTime" format="yyyy年MM月dd日 HH:mm:ss" />

下拉框: <s:select list="#request.role" name="role1" value="roleName" key="id" headerKey="0" headerValue="清選擇角色"></s:select>

3、ajax上傳檔案

ajax上傳檔案主要是使用了ajaxfileupload.js外掛程式,ajax代碼如下所示:

 HTMl代碼:

<input id="fileToUpload" type="file" size="20" name="myFile" class="input"><button type="button" id="buttonUpload" data-dismiss="modal" class="btn btn-primary">上傳</button>

 js代碼:

 

$("#buttonUpload").click(function() {//驗證圖片格式 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("請上傳正確的圖片格式");return;}$.ajaxFileUpload({url : ‘notice_AddImage.action‘,//處理圖片指令碼ecureuri : false,fileElementId : ‘fileToUpload‘,//file控制項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("圖片匯入成功");},error : function(data) {$("#dd").val(data);console.info("error");alert("error");}});});

  

java代碼:

 

 1 // myFile屬性用來封裝上傳的檔案 2     private File myFile; 3  4     // myFileContentType屬性用來封裝上傳檔案的類型 5     private String myFileContentType; 6  7     // myFileFileName屬性用來封裝上傳檔案的檔案名稱 8     private String myFileFileName; 9     InputStream is;10         try {11             is = new FileInputStream(myFile);12             // 設定上傳檔案目錄13             String uploadPath = TemplateUtils.BASEPATH + "\\upload";14             // 重新命名檔案15             String fileName = StringUtils.getUUID() + this.getMyFileFileName().substring(myFileFileName.lastIndexOf("."), myFileFileName.length());16             // 設定目標檔案17             File toFile = new File(uploadPath, fileName);18             // 建立一個輸出資料流19             OutputStream os = new FileOutputStream(toFile);20             // 設定緩衝21             byte[] buffer = new byte[1024];22             int length = 0;23             // 讀取myFile檔案輸出到toFile檔案中24             while ((length = is.read(buffer)) > 0) {25                 os.write(buffer, 0, length);26             }27             // 關閉輸入資料流28             is.close();29             // 關閉輸出資料流30             os.close();

 

  

 

 

  

 

  

SSH總結(二)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.