日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

利用commons-fileupload 上传图片(包含表单数据)

發布時間:2025/6/15 编程问答 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 利用commons-fileupload 上传图片(包含表单数据) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

在一個表單中包含普通文本數據,另外還有需要上傳的圖片,那么本程序將圖片保存到服務器上的一個圖片目錄中,文本數據則獲取然后輸出,查看傳輸是否正確,后面的處理為涉及。

上傳的jsp頁面:

最后將信息輸出:(圖片已經保存在特定目錄中)


需要用到的兩個jar包,commons-fileupload-1.2.2.jar ?commons-io.jar

jsp頁面:

[html] view plaincopy
  • <%@?page?language="java"?import="java.util.*"?pageEncoding="UTF-8"%>??
  • <%??
  • String?path?=?request.getContextPath();??
  • String?basePath?=?request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";??
  • %>??
  • ??
  • <!DOCTYPE?HTML?PUBLIC?"-//W3C//DTD?HTML?4.01?Transitional//EN">??
  • <html>??
  • ??<head>??
  • ????<base?href="<%=basePath%>">??
  • ??????
  • ????<title>My?JSP?'index.jsp'?starting?page</title>??
  • ????<meta?http-equiv="pragma"?content="no-cache">??
  • ????<meta?http-equiv="cache-control"?content="no-cache">??
  • ????<meta?http-equiv="expires"?content="0">??????
  • ????<meta?http-equiv="keywords"?content="keyword1,keyword2,keyword3">??
  • ????<meta?http-equiv="description"?content="This?is?my?page">??
  • ????<!--?
  • ????<link?rel="stylesheet"?type="text/css"?href="styles.css">?
  • ????-->??
  • ??</head>??
  • ????
  • ??<body>??
  • ????<form?id="form5"?method="post"?enctype="multipart/form-data"?<strong>?</strong>?action="ReceiveFile"?>??
  • ?????
  • ????<table?class="rounded-corner?menu1"?id="table_menu5"?border="1px?solid">??
  • ????????<thead?align="center">??
  • ????????????<tr>??
  • ????????????????<th>顯示名稱?</th>??
  • ????????????????<th>鏈接地址</th>??
  • ????????????</tr>??
  • ????????</thead>??
  • ????????<tbody?>??
  • ????????????<tr?class="odd"?>??
  • ????????????????<td?width="20%"?align="center"><input?value="中國青少年宮協會"?readonly="readonly"?name="input_value5"?class="menu5_input1"></td>??
  • ????????????????<td?width="50%"?align="center"><input?value="http://www.cnypa.org/"?readonly="readonly"?style="width:?500px;"?name="input_value5"></td>??
  • ????????????</tr>???
  • ????????????<tr?class="odd"?>??
  • ????????????????<td?width="20%"?align="center"><input?type="file"text"?value="瀏覽"?readonly="readonly"?name="input_value5"?class="menu5_input1"></td>??
  • ????????????????<td?width="50%"?align="center"><input?value=""??style="width:?500px;"?name="input_value5"></td>??
  • ????????????</tr>???
  • ????????????</tbody>??
  • ????????</table>??
  • ??
  • ????<table?class="rounded-corner?menu1"?>??
  • ????????<tfoot?align="right">??
  • ????????<tr>??
  • ????????<td></td>??
  • ????????????<td>??
  • ????????????????<input?type="submit""?value="保存"?id="save5"??onclick="check5();">??
  • ????????????</td>??
  • ????????</tr>??
  • ????????</tfoot>??
  • ????</table>??
  • ??????
  • ??????
  • ?</form>??
  • ??</body>??
  • </html>??

  • servlet后臺程序 [java] view plaincopy
  • package?dai;??
  • ??
  • ??
  • import?java.io.File;??
  • import?java.io.IOException;??
  • import?java.util.ArrayList;??
  • import?java.util.List;??
  • ??
  • import?javax.servlet.ServletException;??
  • import?javax.servlet.http.HttpServlet;??
  • import?javax.servlet.http.HttpServletRequest;??
  • import?javax.servlet.http.HttpServletResponse;??
  • ??
  • import?org.apache.commons.fileupload.FileItem;??
  • import?org.apache.commons.fileupload.disk.DiskFileItemFactory;??
  • import?org.apache.commons.fileupload.servlet.ServletFileUpload;??
  • ??
  • public?class?ReceiveFile?extends?HttpServlet?{??
  • ??????
  • ????private?String?uploadPath?=?"uploadpic/upload/";?//?上傳文件的目錄????
  • ????private?String?tempPath?=?"uploadpic/uploadtmp/";?//?臨時文件目錄????
  • ????private?String?serverPath?=?null;???
  • ????private?String[]?fileType?=?new?String[]{".jpg",".gif",".bmp",".png",".jpeg",".ico"};??
  • ????private?int?sizeMax?=?5;//圖片最大上限????
  • ??????
  • ????@Override??
  • ????protected?void?doPost(HttpServletRequest?request,?HttpServletResponse?response)??
  • ????????????throws?ServletException,?IOException?{??
  • ????????//?服務器端根目錄??
  • ????????String?serverPath?=?getServletContext().getRealPath("/").replace("\\",?"/");????
  • //????????System.out.println(serverPath);??
  • ????????//Servlet初始化時執行,如果上傳文件目錄不存在則自動創建????
  • ????????if(!new?File(serverPath+uploadPath).isDirectory()){???
  • ????????????new?File(serverPath+uploadPath).mkdirs();????
  • ????????}????
  • ????????if(!new?File(serverPath+tempPath).isDirectory()){??
  • ????????????new?File(serverPath+tempPath).mkdirs();??
  • ????????}???
  • ????????DiskFileItemFactory?factory?=?new?DiskFileItemFactory();??
  • ????????factory.setSizeThreshold(5*1024);?//最大緩存????
  • ????????factory.setRepository(new?File(serverPath+tempPath));//臨時文件目錄????
  • ????????????
  • ????????ServletFileUpload?upload?=?new?ServletFileUpload(factory);??
  • ????????upload.setSizeMax(sizeMax*1024*1024);//文件最大上限???
  • ????????????
  • ????????String?filePath?=?null;????
  • ????????try?{????
  • ????????????List<FileItem>?items?=?upload.parseRequest(request);//獲取所有文件列表???
  • ????????????//??
  • ????????????for?(int?i=0;i<items.size();i++)?{??
  • ????????????????//里面一個for循環,獲取一行的數據??
  • ????????????????FileItem?item?=?items.get(i);??
  • <span?style="white-space:pre">??</span>??????????if(!item.isFormField()){//文件名????
  • ????????????????????String?fileName?=?item.getName().toLowerCase();??
  • ????????????????????if(fileName.endsWith(fileType[0])||fileName.endsWith(fileType[1])||fileName.endsWith(fileType[2])||fileName.endsWith(fileType[3])||fileName.endsWith(fileType[4])||fileName.endsWith(fileType[5])){????
  • //????????????????????????String?uuid?=?UUID.randomUUID().toString();????
  • ????????????????????????filePath?=?serverPath+uploadPath+fileName;??
  • //????????????????????????System.out.println(filePath);??
  • ????????????????????????File?file?=?new?File(filePath);??
  • ????????????????????????item.write(file);??
  • ????????????????????????System.out.println(fileName);??
  • ?????????????????????}else?{??
  • ????????????????????????request.setAttribute("errorMsg",?"上傳失敗,請確認上傳的文件存在并且類型是圖片!");??
  • ????????????????????????request.getRequestDispatcher("uploaderror.jsp").forward(request,response);???
  • ????????????????????}??
  • ????????????????}else?{??
  • ??????????????????//非文件流?????
  • ????????????????????String?value=item.getString();??
  • ????????????????????value?=?new?String(value.getBytes("ISO-8859-1"),"UTF-8");??
  • //????????????????????System.out.println(value);??
  • ????????????????????System.out.println(value);??
  • ????????????????}??
  • ??????????????????
  • ????????????}???
  • ????????}?catch?(Exception?e)?{??
  • ????????????e.printStackTrace();????
  • ????????????request.setAttribute("errorMsg",?"上傳失敗,請確認上傳的文件存在并且類型是圖片!");??
  • ????????????request.getRequestDispatcher("uploaderror.jsp").forward(request,response);???
  • ????????}??
  • ????}??
  • ??????
  • ????@Override??
  • ????protected?void?doGet(HttpServletRequest?req,?HttpServletResponse?resp)??
  • ????????????throws?ServletException,?IOException?{??
  • ????????this.doPost(req,?resp);??
  • ????}??
  • }??

  • 另外,文章中有幾個注意點,像form表單的enctype="multipart/form-data" 需要注意,然后后臺處理的時候需要將文件和普通文本數據分開處理,先說的大概,詳細的下次再來編輯,源碼的話我上傳到這里:http://download.csdn.net/detail/xiaobaismiley/6333407

    總結

    以上是生活随笔為你收集整理的利用commons-fileupload 上传图片(包含表单数据)的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。