1. 先在頁面處配置上傳按鈕:html
form中須要添加 enctype="multipart/form-data" 參數,以下java
<html:form action="/menuSmart.shtml" enctype="multipart/form-data" method="post"> post
如下爲上傳按鈕,並外加顯示鏈接ui
- <td class="dvtCellLabel">菜單圖片</td>
- <td class="dvtCellInfo">
- <html:file property="iconFile" />
- <html:hidden property="icon"/>
- <logic:notEmpty name="menuSmartForm" property="icon">
- <a href="${menuSmartForm.icon}" target="_blank">查看</a>
- </logic:notEmpty>
- d>
<td class="dvtCellLabel">菜單圖片</td> <td class="dvtCellInfo"> <html:file property="iconFile" /> <html:hidden property="icon"/> <logic:notEmpty name="menuSmartForm" property="icon"> <a href="${menuSmartForm.icon}" target="_blank">查看</a> </logic:notEmpty> </td>
2. 如下爲ACTION中的上傳文件操做代碼:spa
- /**
- * 文件上傳
- * @param form
- * @param request
- * @param isUpdate
- * @return
- * @throws Exception
- */
- protected String simpleUpload(ActionForm form, HttpServletRequest request, boolean isUpdate) throws Exception {
- MenuForm menuForm = (MenuForm) form;
- String result="" ;
- FormFile file = null;
- FormFile[] files = new FormFile[]{menuForm.getIconFile(),menuForm.getBgImgFile()};
- for (int i = 0; i < files.length; i++) {
- file = files[i];
- if (file != null && !file.getFileName().equals("")) {
- String endName = FileUtil.getFileExt(file.getFileName());
- if(!(endName.toUpperCase().equals("PNG")||endName.toUpperCase().equals("JPG")||endName.toUpperCase().equals("GIF"))){
- log.info("上傳的圖標文件不是圖片格式");
- return null;
- }
- /*上至上傳相對路徑*/
- String uploadPath = ("upload" + File.separator );
- /*設置本地絕對路徑*/
- String uploadDir = servlet.getServletContext().getRealPath(uploadPath);
- try {
- /*建立文件流信息*/
- File nwFile = new File(uploadDir);
- /*判斷是否存在改目錄文件夾*/
- if (!nwFile.exists())
- nwFile.mkdirs();
- long uid = System.currentTimeMillis();
- /*獲取文件後綴名*/
- String fileExt = "." + endName;
- String fname = uploadDir + File.separator + uid + fileExt;
- InputStream streamIn = file.getInputStream();
- OutputStream streamOut = new FileOutputStream(fname);
- int bytesRead = 0;
- byte[] buffer = new byte[8192];
- /*開始寫文件*/
- while ((bytesRead = streamIn.read(buffer, 0, 8192)) != -1) {
- streamOut.write(buffer, 0, bytesRead);
- }
- streamOut.close();
- streamIn.close();
- file.destroy();
- String realName = "upload" + "/"+ uid + fileExt;
- /*將真實的文件名設置到FORM中*/
- if(i==0){
- menuForm.setIcon(realName);
- }
- if(i==1){
- menuForm.setBgImg(realName);
- }
- result = "upload" + File.separator + uid + fileExt;
- } catch (Exception e) {
- log.error("上傳圖片信息是發生異常:"+e);
- return null;
- }
- }
- }
- return result;
- }
/** * 文件上傳 * @param form * @param request * @param isUpdate *
@return * @throws Exception */ protected String simpleUpload(ActionForm form, HttpServletRequest request, boolean isUpdate) throws Exception { MenuForm menuForm = (MenuForm) form; String result="" ; FormFile file = null; FormFile[] files = new FormFile[]{menuForm.getIconFile(),menuForm.getBgImgFile()}; for (int i = 0; i < files.length; i++) { file = files[i]; if (file != null && !file.getFileName().equals("")) { String endName = FileUtil.getFileExt(file.getFileName()); if(!(endName.toUpperCase().equals("PNG")||endName.toUpperCase().equals("JPG")||endName.toUpperCase().equals("GIF"))){ log.info("上傳的圖標文件不是圖片格式"); return null; } /*上至上傳相對路徑*/ String uploadPath = ("upload" + File.separator ); /*設置本地絕對路徑*/ String uploadDir = servlet.getServletContext().getRealPath(uploadPath); try { /*建立文件流信息*/ File nwFile = new File(uploadDir); /*判斷是否存在改目錄文件夾*/ if (!nwFile.exists()) nwFile.mkdirs(); long uid = System.currentTimeMillis(); /*獲取文件後綴名*/ String fileExt = "." + endName; String fname = uploadDir + File.separator + uid + fileExt; InputStream streamIn = file.getInputStream(); OutputStream streamOut = new FileOutputStream(fname); int bytesRead = 0; byte[] buffer = new byte[8192]; /*開始寫文件*/ while ((bytesRead = streamIn.read(buffer, 0, 8192)) != -1) { streamOut.write(buffer, 0, bytesRead); } streamOut.close(); streamIn.close(); file.destroy(); String realName = "upload" + "/"+ uid + fileExt; /*將真實的文件名設置到FORM中*/ if(i==0){ menuForm.setIcon(realName); } if(i==1){ menuForm.setBgImg(realName); } result = "upload" + File.separator + uid + fileExt; } catch (Exception e) { log.error("上傳圖片信息是發生異常:"+e); return null; } } } return result; }
3. 若是還須要對文檔進行解壓縮操做,以及其餘的操做,請參考 本博客中標題爲 「AJAX上傳下載文件」的文章。.net