文章出處
<input type="file" name="upload" />
method="post" enctype="multipart/form-data"
* struts2框架中文件上傳:
* 文件的條件:
* 表單提交的方式需要是POST
* 表單中上傳項必須有一個name屬性.
* 表單的enctype屬性值需要是multipart/form-data
// 文件上傳需要的三個屬性: private File upload; private String uploadFileName; private String uploadContentType; public void setUpload(File upload) { this.upload = upload; } public void setUploadFileName(String uploadFileName) { this.uploadFileName = uploadFileName; } public void setUploadContentType(String uploadContentType) { this.uploadContentType = uploadContentType; }
// 保存商品的方法://import org.apache.commons.io.FileUtils; public String save() throws IOException { // 將提交的數據添加到數據庫中. product.setPdate(new Date()); // product.setImage(image); if(upload != null){ // 將商品圖片上傳到服務器上. // 獲得上傳圖片的服務器端路徑. String path = ServletActionContext.getServletContext().getRealPath( "/products"); // 創建文件類型對象: File diskFile = new File(path + "//" + uploadFileName); // 文件上傳: FileUtils.copyFile(upload, diskFile); product.setImage("products/" + uploadFileName); } productService.save(product); return "saveSuccess"; }
看文倉www.kanwencang.com網友整理上傳,為您提供最全的知識大全,期待您的分享,轉載請注明出處。
歡迎轉載:http://www.kanwencang.com/bangong/20170123/93897.html
文章列表
全站熱搜