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

歡迎訪問 生活随笔!

生活随笔

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

编程问答

上传图片或文件 方法一

發布時間:2023/12/1 编程问答 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 上传图片或文件 方法一 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

?最近在鞏固一些知識點,回過頭看之前做過的項目,所以就在這里總結一下

話不多說,直接看源碼

?

前端

publish-menu.jsp

1 <form action="PublishMenu" method="post" enctype="multipart/form-data" > 2 3 <input name="menu_picture" type="file" id="file_input" style="display: none;" onchange="preImg(this.id,'photo_finish')" /> 4 <label for="file_input"> 5 <img id="photo_finish" src="img/finished_menu.jpg" width="210px" height="250px" style="margin-right: 20px;" /> 6 </label> 7 8 </form>

show-img.js

1 function getFileUrl(sourceId) { 2 var url; 3 if(navigator.userAgent.indexOf("MSIE") >= 1) { // IE 4 url = document.getElementById(sourceId).value; 5 } else if(navigator.userAgent.indexOf("Firefox") > 0) { // Firefox 6 url = window.URL.createObjectURL(document.getElementById(sourceId).files.item(0)); 7 } else if(navigator.userAgent.indexOf("Chrome") > 0) { // Chrome 8 url = window.URL.createObjectURL(document.getElementById(sourceId).files.item(0)); 9 } 10 return url; 11 } 12 13 function preImg(sourceId, targetId) { 14 var url = getFileUrl(sourceId); 15 var imgPre = document.getElementById(targetId); 16 imgPre.src = url; 17 /* alert("圖片路徑獲取前!"); 18 var p = url.getPath(); //將圖片的src變為獲取到的路徑 19 alert("圖片路徑!" + p); */ 20 }

?

后臺

package?com.dgx.MenuShare.servlet;
import
java.io.File; import java.io.IOException; 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.FileUploadException; import org.apache.commons.fileupload.disk.DiskFileItemFactory; import org.apache.commons.fileupload.servlet.ServletFileUpload;import com.dl.MenuShare.service.MenuService;@SuppressWarnings("serial") public class PublishMenuServlet extends HttpServlet { @Overrideprotected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {// 設置請求對象以utf-8的形式接受表單數據req.setCharacterEncoding("utf-8");resp.setCharacterEncoding("utf-8");int x = 0 ;int y = 0 ;String text[][] = new String[16][2] ; //準備用來存儲其他非文件的信息String img[][] = new String[9][2] ; //準備用來存儲文件的信息//新建上傳工廠DiskFileItemFactory factory = new DiskFileItemFactory(); //設置文件大小限制。//如果小于該文件限制,則文件直接保存在內存中//如果大于該文件限制,則文件將保存到設置的臨時目錄中。factory.setSizeThreshold(1024 * 1024);//getServletContext().getRealPath() 的用法 //比如s參數'/temp'表示根路徑下的temp文件夾'返回一個File類型'你可以用判斷它是否存在'不存在就建立'存在就用它 //設置臨時目錄factory.setRepository(new File(getServletContext().getRealPath("/temp")));//實例化一個ServletFileUpload對象用來上傳ServletFileUpload sfu = new ServletFileUpload(factory);//得到所有的表單項List<FileItem> all = null;try {all = sfu.parseRequest(req);} catch (FileUploadException e) {e.printStackTrace();}for(FileItem item : all) {//如果是普通表單字段if(item.isFormField()) {//獲得該字段名稱String name = item.getFieldName();System.out.println("字段名稱:"+name); //獲得該字段值String value = item.getString("utf-8");System.out.println("字段值:"+value); text[x][0] = name ;text[x][1] = value ;System.out.println("數組text"+x+":"+text[x][0]+" "+text[x][1]);x++ ;//將值設置到request屬性范圍中 req.setAttribute(name, value);} //如果為文件域else {//取得文件域字段名String name = item.getFieldName();System.out.println("文件域字段名:"+name); //取得文件名String value = item.getName();System.out.println("文件名:"+value); //截取文件名int begin = value.lastIndexOf("\\");value = value.substring(begin + 1);img[y][0] = name ; if ( value!=null && value!="" ) {//將值設置到request屬性范圍中 req.setAttribute(name, value); //設置上傳文件目錄String path = req.getRealPath("/"); //獲取服務器下路徑path = path.replaceAll("\\\\", "/"); String uploadPath = path + "/upload" ;File file = new File(uploadPath);if (!file.exists()) {new File(uploadPath).mkdir();}//寫入文件try {if ( y==0 ) {item.write(new File(uploadPath,text[0][1]+".jpg"));img[y][1] = "upload/"+text[0][1]+".jpg" ; }else {item.write(new File(uploadPath,text[0][1]+"步驟"+y+".jpg"));img[y][1] = "upload/"+text[0][1]+"步驟"+y+".jpg" ;} System.out.println("數組img"+y+":"+img[y][0]+" "+img[y][1]);y++ ;} catch (Exception e) {e.printStackTrace();}}else {img[y][1] = "" ;} } }

      ......

      String menu_picture = img[0][1] ;
      String picture_one = img[1][1] ;?
      ......
      MenuService menuService = new MenuService() ;

把 圖片文件路徑 和其他信息 存進DBint i = menuService.AddMenuInfoService( ...... ) ;if ( i!=0 ) {req.setAttribute("massger", "上傳圖片文件成功");}else {req.setAttribute("massger", "上傳圖片文件失敗");
     }
     //上傳完成后執行跳轉
     req.getRequestDispatcher("XXX.jsp").forward(req, resp);
  }
}

?

?

共同學習,共同進步,若有補充,歡迎指出,謝謝!

?

轉載于:https://www.cnblogs.com/dengguangxue/p/10757601.html

創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎

總結

以上是生活随笔為你收集整理的上传图片或文件 方法一的全部內容,希望文章能夠幫你解決所遇到的問題。

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