最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

java圖片和文本同時(shí)提交到表單的實(shí)例代碼

 更新時(shí)間:2020年02月03日 10:15:37   作者:V  
在本篇文章里小編給大家整理的是關(guān)于java實(shí)現(xiàn)圖片和文本同時(shí)提交到表單的相關(guān)內(nèi)容,有需要的朋友們可以學(xué)習(xí)下。

首先來(lái)看如下效果圖片:

表單代碼:

<form action="/addPro" method="post" enctype="multipart/form-data">

 <a>寵物(或產(chǎn)品)類型:</a><select id="categoryID" name="cid"></select><br/><br/>

 <a>寵物(或產(chǎn)品)名字:</a><input type="text" name="cname"><br/><br/>

 <a>一句話介紹:</a><input type="text" name="introduction"><br/><br/>

 <a>題目:</a><input type="text" name="title"><br/><br/>

 <a>價(jià)錢:</a><input type="text" name="price"><br/><br/>

 <a>庫(kù)存:</a><input type="text" name="stock"><br/><br/>

 <a>狀態(tài):</a><select name="status">

  <option value="1">在售</option>

  <option value="2">下架</option>

  <option value="3">刪除</option>

 </select><br/><br/>

 <a>頭像設(shè)置:</a><input type="file" οnchange="previewFile()" name="fileName">

 <br/>

 <img src="${data.image}" alt="Image preview"/><br/>

 <a>詳細(xì)描述(編輯完需要在文本框右上角點(diǎn)保存):</a><br/>

 <div id="editor">

  <p>商品詳細(xì)描述</p>

  <p>編輯完需要在文本框右上角點(diǎn)保存</p>

 </div><input type="hidden" name="details" id="detail"><br/><br/>

 <input type="submit" value="新增商品">

</form>

提交表單是采用二進(jìn)制方式提交,所以一般用來(lái)上傳圖片操作,當(dāng)在這個(gè)表單下同時(shí)上傳文本,就會(huì)報(bào)錯(cuò)。但是業(yè)務(wù)需要上傳商品是文本和圖片同時(shí)上傳的,所以這里要用到commons的四個(gè)包,使用Maven導(dǎo)入,如下:

<!-- https://mvnrepository.com/artifact/commons-io/commons-io有關(guān)圖片文本同時(shí)上傳 -->

 <dependency>

  <groupId>commons-io</groupId>

  <artifactId>commons-io</artifactId>

  <version>2.4</version>

 </dependency>

 <!-- https://mvnrepository.com/artifact/commons-fileupload/commons-fileupload -->

 <dependency>

  <groupId>commons-fileupload</groupId>

  <artifactId>commons-fileupload</artifactId>

  <version>1.3.3</version>

 </dependency>

 <!-- https://mvnrepository.com/artifact/commons-collections/commons-collections -->

 <dependency>

  <groupId>commons-collections</groupId>

  <artifactId>commons-collections</artifactId>

  <version>3.1</version>

 </dependency>

 <!-- https://mvnrepository.com/artifact/commons-beanutils/commons-beanutils -->

 <dependency>

  <groupId>commons-beanutils</groupId>

  <artifactId>commons-beanutils</artifactId>

  <version>1.9.2</version>

 </dependency>

Java代碼如下:

主要判斷每一個(gè)參數(shù)的屬性,圖片的則進(jìn)行圖片處理,文本則進(jìn)行文本處理。

//新增產(chǎn)品

 @RequestMapping("/addPro")

 public void addPro(HttpServletRequest request, HttpServletResponse response) throws IOException {

  //編碼規(guī)范

  response.setContentType("text/html");

//  response.setCharacterEncoding("utf-8");

  Product product = new Product();

 

  //這種方法主要通過(guò)if (item.isFormField())這個(gè)條件判別文件還是非文件

  DiskFileItemFactory factory = new DiskFileItemFactory();

  ServletFileUpload upload = new ServletFileUpload(factory);

  List items = null;

  try {

   items = upload.parseRequest(request);

  } catch (FileUploadException e) {

   e.printStackTrace();

  } // 解析request請(qǐng)求

  Iterator iter = items.iterator();// 遍歷表單中提交過(guò)來(lái)的內(nèi)容

  while (iter.hasNext()) {

   FileItem item = (FileItem) iter.next();

   if (item.isFormField()) { // 如果是表單域 ,就是非文件上傳元素

    String value = item.getString("UTF-8"); // 獲取value屬性的值,這里需要指明UTF-8格式,否則出現(xiàn)中文亂碼問(wèn)題

    if (item.getFieldName().equals("cid")) {// 對(duì)應(yīng)form中屬性的名字

     int categoryId = Integer.parseInt(value);

     product.setCategory_id(categoryId);

    } else if (item.getFieldName().equals("cname")) {

     product.setName(value);

    }else if (item.getFieldName().equals("introduction")) {

     product.setIntroduction(value);

    }else if (item.getFieldName().equals("title")) {

     product.setTitle(value);

    }else if (item.getFieldName().equals("price")) {

     BigDecimal price=new BigDecimal(value);

     product.setPrice(price);

    }else if (item.getFieldName().equals("stock")) {

     product.setStock(Integer.parseInt(value));

    }else if (item.getFieldName().equals("status")) {

     product.setStatus(Integer.parseInt(value));

    }else if (item.getFieldName().equals("details")) {

     product.setDetail(value);

    }

   }else {

    String filename = item.getName(); // 文件的名字

 

    String imgname = filename.substring(0, filename.indexOf(".")); //減去“.”后面的字符

 

    //tomcat啟動(dòng)位置

//    String t1 = System.getProperty("user.dir").substring(0,

//      System.getProperty("user.dir").length() - 4);

 

    String path = request.getServletContext().getRealPath("img"); //target找到img位置

    Long time = Calendar.getInstance().getTimeInMillis(); //時(shí)間戳,保證文件命名不重復(fù)

    String imgurl = "./img/"+imgname+time+".jpg";

    product.setImage(imgurl);

    System.out.println(imgurl);

    File saveFile = new File(path+"/" + imgname+time+".jpg"); // 定義一個(gè)file指向一個(gè)具體的文件

    try {

     item.write(saveFile);// 把上傳的內(nèi)容寫到一個(gè)文件中

     System.out.println("上傳到"+path+"成功");

    } catch (Exception e) {

     /* e.printStackTrace(); */

     System.out.println("文件"+path+"為空");

    }

   }

  }

 

  if(productDaoService.addProduct(product)){

   PrintWriter out = response.getWriter();

   out.print("<script language=\"javascript\">alert('ADD SUCCESS');window.location.href='/admin/administrator'</script>");

  }else {

   PrintWriter out = response.getWriter();

   out.print("<script language=\"javascript\">alert('增加失敗');window.location.href='/admin/addProduct'</script>");

  }

 }

以上就是java實(shí)現(xiàn)圖片和文本同時(shí)提交到表單的詳細(xì)內(nèi)容,感謝大家的學(xué)習(xí)和對(duì)腳本之家的支持。

相關(guān)文章

  • 搭建Spring MVC和Vue3的應(yīng)用程序的實(shí)現(xiàn)

    搭建Spring MVC和Vue3的應(yīng)用程序的實(shí)現(xiàn)

    本文主要介紹了搭建Spring MVC和Vue3的應(yīng)用程序的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2023-11-11
  • python實(shí)現(xiàn)高斯模糊及原理詳解

    python實(shí)現(xiàn)高斯模糊及原理詳解

    高斯模糊是一種常見(jiàn)的模糊技術(shù),本文主要介紹了python實(shí)現(xiàn)高斯模糊及原理詳解,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-05-05
  • Java VisualVM監(jiān)控遠(yuǎn)程JVM(詳解)

    Java VisualVM監(jiān)控遠(yuǎn)程JVM(詳解)

    下面小編就為大家?guī)?lái)一篇Java VisualVM監(jiān)控遠(yuǎn)程JVM(詳解)。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2017-10-10
  • Java數(shù)組去重復(fù)的18種方法示例

    Java數(shù)組去重復(fù)的18種方法示例

    這篇文章主要為大家介紹了Java數(shù)組去重復(fù)的18種寫法示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-08-08
  • Java使用Statement接口執(zhí)行SQL語(yǔ)句操作實(shí)例分析

    Java使用Statement接口執(zhí)行SQL語(yǔ)句操作實(shí)例分析

    這篇文章主要介紹了Java使用Statement接口執(zhí)行SQL語(yǔ)句操作,結(jié)合實(shí)例形式詳細(xì)分析了Java使用Statement接口針對(duì)mysql數(shù)據(jù)庫(kù)進(jìn)行連接與執(zhí)行SQL語(yǔ)句增刪改查等相關(guān)操作技巧與注意事項(xiàng),需要的朋友可以參考下
    2018-07-07
  • 基于@PostConstruct注解的使用,解決向靜態(tài)變量注入值

    基于@PostConstruct注解的使用,解決向靜態(tài)變量注入值

    這篇文章主要介紹了基于@PostConstruct注解的使用,解決向靜態(tài)變量注入值問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-09-09
  • Java Clone(類的復(fù)制)實(shí)例代碼

    Java Clone(類的復(fù)制)實(shí)例代碼

    Java Clone(類的復(fù)制)實(shí)例代碼,需要的朋友可以參考一下
    2013-03-03
  • 使用IDEA如何打包發(fā)布SpringBoot并部署到云服務(wù)器

    使用IDEA如何打包發(fā)布SpringBoot并部署到云服務(wù)器

    這篇文章主要介紹了使用IDEA如何打包發(fā)布SpringBoot并部署到云服務(wù)器問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-12-12
  • Spring中@Value設(shè)置默認(rèn)值問(wèn)題解決

    Spring中@Value設(shè)置默認(rèn)值問(wèn)題解決

    本文主要介紹了Spring中@Value設(shè)置默認(rèn)值問(wèn)題解決,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2023-07-07
  • 最好的Java 反編譯工具的使用對(duì)比分析

    最好的Java 反編譯工具的使用對(duì)比分析

    恰好最近工作中也需要用到 Java 反編譯,所以這篇文章介紹目前常見(jiàn)的的幾種 Java 反編譯工具的使用,在文章的最后也會(huì)通過(guò)編譯速度、語(yǔ)法支持以及代碼可讀性三個(gè)維度,對(duì)它們進(jìn)行測(cè)試,分析幾款工具的優(yōu)缺點(diǎn),感興趣的朋友一起看看吧
    2021-05-05

最新評(píng)論

邮箱| 逊克县| 鸡泽县| 荔浦县| 扬中市| 辽阳县| 宝山区| 东方市| 罗平县| 曲松县| 民权县| 德江县| 玉林市| 资溪县| 辛集市| 金塔县| 湖北省| 平阳县| 涞水县| 渑池县| 昭觉县| 邵武市| 扎赉特旗| 丘北县| 海阳市| 满城县| 平昌县| 漠河县| 项城市| 岳池县| 泾川县| 泽普县| 敦化市| 英德市| 汶川县| 堆龙德庆县| 昭通市| 呼伦贝尔市| 炎陵县| 濮阳县| 泸溪县|