今天就跟大家聊聊有關(guān)java中run/debug configurations上傳圖片文件的示例分析,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結(jié)了以下內(nèi)容,希望大家根據(jù)這篇文章可以有所收獲。
創(chuàng)新互聯(lián)公司是一家專注于成都網(wǎng)站設(shè)計、成都網(wǎng)站制作與策劃設(shè)計,泗陽網(wǎng)站建設(shè)哪家好?創(chuàng)新互聯(lián)公司做網(wǎng)站,專注于網(wǎng)站建設(shè)10余年,網(wǎng)設(shè)計領(lǐng)域的專業(yè)建站公司;建站業(yè)務(wù)涵蓋:泗陽等地區(qū)。泗陽做網(wǎng)站價格咨詢:18982081108
上傳圖片步驟:
1.設(shè)置圖片服務(wù)器(在tomcat中加入虛擬路徑)
2.導(dǎo)入依賴
需要導(dǎo)入io和fileupload
<dependency> <groupId>commons-io</groupId> <artifactId>commons-io</artifactId> <version>2.4</version></dependency><dependency> <groupId>commons-fileupload</groupId> <artifactId>commons-fileupload</artifactId> <version>1.2.2</version></dependency>
3.修改jsp頁面 method的請求必須為post請求,并且必須加上enctype="multipart/form-data" ,并將type為file的input標簽的name值改為pictureFile改(這個名字不會出問題,也可以使用其他名字)
<form action="${pageContext.request.contextPath}/add" method="post" enctype="multipart/form-data">服裝名稱:<input type="text" name="name"><br>商品價格:<input type="text" name="price"><br>供應(yīng)商:<input type="text" name="pro"><br>進貨時間:<input type="date" name="getTime"><br>進貨數(shù)量:<input type="number" name="getNumber"><br>商品圖片:<input type="file" name="pictureFile"><br><input type="submit" value="添加"></form>
4.在springmvc.xml中加入上傳文件解析器(解析器的id一定要固定)
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">//最大上傳大小<property name="maxUploadSize" value="5000000"/></bean>
5.從Controller寫上傳的代碼
@RequestMapping("add") //MultipartFile進行參數(shù)綁定時與前端頁面的input的name屬性值一致public String add(Clothing clothing, MultipartFile pictureFile){if(pictureFile.getSize()>0){ //根據(jù)上傳文件的對象獲取文件的名稱 String pname=pictureFile.getOriginalFilename(); //重新設(shè)置文件的名稱(uuid) String name= UUID.randomUUID().toString().replace("-","").toUpperCase()+pname.substring(pname.lastIndexOf("."));try { //上傳文件(文件路徑+文件名稱)(new File("D:\\imgs\\"+圖片全名稱)) pictureFile.transferTo(new File("D:\\imgs\\exam12_21\\"+name)); } catch (IOException e) { e.printStackTrace(); } //把文件名設(shè)置到pojo clothing.setImg(name); }try { //進行數(shù)據(jù)庫操作cloService.add(clothing); } catch (SQLException e) { e.printStackTrace(); }return "redirect:/findAll";
用到的實體類
public class Clothing { private Integer id; private String name; private Double price; private String pro; @DateTimeFormat(pattern = "yyyy-MM-dd") private Date getTime; private Integer getNumber; private String img; public Integer getId() {return id; } public void setId(Integer id) {this.id = id; } public String getName() {return name; } public void setName(String name) {this.name = name; } public Double getPrice() {return price; } public void setPrice(Double price) {this.price = price; } public String getPro() {return pro; } public void setPro(String pro) {this.pro = pro; } public Date getGetTime() {return getTime; } public void setGetTime(Date getTime) {this.getTime = getTime; } public Integer getGetNumber() {return getNumber; } public void setGetNumber(Integer getNumber) {this.getNumber = getNumber; } public String getImg() {return img; } public void setImg(String img) {this.img = img; } }
看完上述內(nèi)容,你們對java中run/debug configurations上傳圖片文件的示例分析有進一步的了解嗎?如果還想了解更多知識或者相關(guān)內(nèi)容,請關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝大家的支持。
本文題目:java中run/debugconfigurations上傳圖片文件的示例分析
文章地址:http://m.newbst.com/article28/jeiojp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供虛擬主機、定制網(wǎng)站、面包屑導(dǎo)航、品牌網(wǎng)站建設(shè)、靜態(tài)網(wǎng)站、網(wǎng)站設(shè)計公司
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)