免费视频淫片aa毛片_日韩高清在线亚洲专区vr_日韩大片免费观看视频播放_亚洲欧美国产精品完整版

打開APP
userphoto
未登錄

開通VIP,暢享免費電子書等14項超值服

開通VIP
利用struts2框架實現(xiàn)當(dāng)用戶上傳圖片到服務(wù)器后,再顯示到前臺頁面中
關(guān)閉


目錄(?)[+]

分析:由于struts2已經(jīng)集成了fileupload,而且用起來要比直接使用更加方便,所以當(dāng)實現(xiàn)文件上傳功能的時候最好選擇struts2

1、開發(fā)前的準備:

導(dǎo)入struts2的jar包、struts.xml配置文件、同時將web.xml配置好。(此過程比較容易,這里不在說明)

2、action代碼的編寫(實現(xiàn)文件上傳的功能,并轉(zhuǎn)發(fā)到顯示照片的頁面中)

  1. package com.xiaojie.upload;  
  2.   
  3. import java.io.File;  
  4. import java.io.FileInputStream;  
  5. import java.io.FileOutputStream;  
  6. import java.util.Date;  
  7. import java.util.Map;  
  8.   
  9. import javax.servlet.ServletContext;  
  10.   
  11. import org.apache.struts2.ServletActionContext;  
  12. import org.apache.struts2.interceptor.RequestAware;  
  13.   
  14. import com.opensymphony.xwork2.ActionSupport;  
  15. import com.xiaojie.service.ProductService;  
  16.   
  17. public class UploadAction extends ActionSupport implements RequestAware {  
  18.     /** 
  19.      *  
  20.      */  
  21.     private static final long serialVersionUID = 1L;  
  22.     private ProductService productService=new ProductService();  
  23.     private File wj;  
  24.     private String wjContentType;//文件類型  
  25.     private String wjFileName;//文件名  
  26.     private Map<String, Object> request;  
  27.     private String wjName;  
  28.     public String getWjName() {  
  29.         return wjName;  
  30.     }  
  31.     public void setWjName(String wjName) {  
  32.         this.wjName = wjName;  
  33.     }  
  34.     private String wjDesc;  
  35.       
  36.     public File getWj() {  
  37.         return wj;  
  38.     }  
  39.     public void setWj(File wj) {  
  40.         this.wj = wj;  
  41.     }  
  42.     public String getWjContentType() {  
  43.         return wjContentType;  
  44.     }  
  45.     public void setWjContentType(String wjContentType) {  
  46.         this.wjContentType = wjContentType;  
  47.     }  
  48.     public String getWjFileName() {  
  49.         return wjFileName;  
  50.     }  
  51.     public void setWjFileName(String wjFileName) {  
  52.         this.wjFileName = wjFileName;  
  53.     }  
  54.     public String getWjDesc() {  
  55.         return wjDesc;  
  56.     }  
  57.     public void setWjDesc(String wjDesc) {  
  58.         this.wjDesc = wjDesc;  
  59.     }  
  60.     @Override  
  61.     public String execute() throws Exception {  
  62.         // TODO Auto-generated method stub  
  63.         //修改文件名  
  64.         wjFileName=new Date().getTime()+wjFileName;  
  65.         ServletContext servletContext=ServletActionContext.getServletContext();  
  66.         String path=servletContext.getRealPath("/images/"+wjFileName);//文件最終要上傳到的路徑  
  67.         FileOutputStream out=new FileOutputStream(path);  
  68.         FileInputStream in=new FileInputStream(wj);  
  69.         byte[]buffer=new byte[1024];  
  70.         int len=0;  
  71.         while((len=in.read(buffer))!=-1){  
  72.             out.write(buffer,0,len);  
  73.               
  74.         }  
  75.         out.close();  
  76.         in.close();  
  77.         String image="images/"+wjFileName;  
  78.         request.put("image", image);  
  79.         return super.execute();  
  80.     }  
  81.     @Override  
  82.     public void setRequest(Map<String, Object> arg0) {  
  83.         // TODO Auto-generated method stub  
  84.         this.request=arg0;  
  85.     }  
  86. }  

3、文件上傳頁面代碼和圖片顯示頁面代碼

3.1文件上傳頁面:

  1. <%@ page language="java" contentType="text/html; charset=UTF-8"  
  2.     pageEncoding="UTF-8"%>  
  3.    <%@ taglib prefix="s" uri="/struts-tags" %>  
  4. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">  
  5. <html>  
  6. <head>  
  7. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">  
  8. <title>Insert title here</title>  
  9. </head>  
  10. <body>  
  11.     <form action="upload" method="post" enctype="multipart/form-data">  
  12.         商品圖片:<input type="file" name="wj"/><br>  
  13.         商品名稱:<input type="text" name="wjName"/>  
  14.         商品描述:<input type="text" name="wjDesc"/>  
  15.         <input type="submit" value="提交"/>  
  16.     </form>  
  17.   
  18. </body>  
  19. </html>  
注意:圖片會上傳到你在tomcat上所部署的目錄文件夾下的images文件夾下:(是不是多了一張圖片?)


3.2、圖片顯示頁面代碼:

  1. <%@ page language="java" contentType="text/html; charset=UTF-8"  
  2.     pageEncoding="UTF-8"%>  
  3.   
  4. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">  
  5. <html>  
  6. <head>  
  7.   
  8. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">  
  9. <title>Insert title here</title>  
  10. </head>  
  11. <body>  
  12. 圖片上傳成功  
  13. <img alt="" src="${requestScope.image } " >http://localhost:8080/flieupload/${requestScope.image }  
  14. </body>  
  15. </html>  

特別注意:由于用戶很有可能會上傳中文名稱的圖片,這會導(dǎo)致圖片無法在前臺頁面上顯示出來。這個問題怎么解決呢?

在tomcat的server.xml中加入URIEncoding="utf-8"(網(wǎng)頁的編碼是utf-8)<Connector port="8080" connectionTimeout="20000" disableUploadTimeout="true" URIEncoding="utf-8" />

本站僅提供存儲服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊舉報。
打開APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
struts2.0簡單的例子
jquery之a(chǎn)jaxfileupload異步上傳插件
Struts2 類型轉(zhuǎn)換
struts-2.1.8.1與spring2.5.6的整合
Struts2下使用Common
struts2之請求參數(shù)接收
更多類似文章 >>
生活服務(wù)
分享 收藏 導(dǎo)長圖 關(guān)注 下載文章
綁定賬號成功
后續(xù)可登錄賬號暢享VIP特權(quán)!
如果VIP功能使用有故障,
可點擊這里聯(lián)系客服!

聯(lián)系客服