- 準備:導入commons-io-2.0.1.jar和commons-fileupload-1.2.1.jar,版本可以不一樣
- 前臺:
- <struts:form action="weiboAction!uploadPicture.shtml" enctype="multipart/form-data" namespace="/" method="post">
- <struts:file name="image" label="文件"></struts:file>
- <struts:submit value="上傳"/>
- </struts:form>
- 後臺:
- /**
- * 作者:劉鵬
- * 時間:2013-07-07
- * 描述:微博列表中的圖片和文件上傳顯示
- * @return
- */
- /*****************以下爲上傳部分*******************************/
- private File image; //得到上傳的文件
- private String imageFileName; //得到文件的名稱,寫法是固定的
- private String imageContentType; //得到文件的類型
- public String getImageContentType() {
- return imageContentType;
- }
- public void setImageContentType(String imageContentType) {
- this.imageContentType = imageContentType;
- }
- public String getImageFileName() {
- return imageFileName;
- }
- public void setImageFileName(String imageFileName) {
- this.imageFileName = imageFileName;
- }
- public File getImage() {
- return image;
- }
- public void setImage(File image) {
- this.image = image;
- }
- public String addUI(){
- return SUCCESS;
- }
- public String uploadPicture(){
- Weibo model = getModel();
- Date date = new Date();
- SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
- String dateString = simpleDateFormat.format(date);
- model.setTime(dateString);
- HttpServletRequest request = ServletActionContext.getRequest() ;
- HttpServletResponse response = ServletActionContext.getResponse();
- //以下爲圖片上傳部分
- //保存到根目錄下的uploadImages文件夾下
- String realPath = ServletActionContext.getServletContext().getRealPath("/uploadImages"); //取得真實路徑
- System.out.println(realPath);
- System.out.println(imageFileName);
- System.out.println(imageContentType);
- //自動命名
- if(imageFileName!=null && imageFileName.length()!=0){
- Random random = new Random(99999);
- int tempInt = random.nextInt();
- Date datenew = new Date();
- SimpleDateFormat simpleDateFormatnew = new SimpleDateFormat("yyyyMMddhhmmss");
- int last = imageFileName.lastIndexOf(".");
- String head = imageFileName.substring(0,last);
- String type = imageFileName.substring(last);
- imageFileName = simpleDateFormatnew.format(datenew) + tempInt + type;
- System.out.println("新的文件名稱是:"+imageFileName);
- //創建父文件夾
- if(image!=null){
- File saveFile = new File(new File(realPath), imageFileName);
- if(!saveFile.getParentFile().exists()){ //如果Images文件夾不存在
- saveFile.getParentFile().mkdirs(); //則創建新的多級文件夾
- }
- try {
- FileUtils.copyFile(image, saveFile); //保存文件
- ActionContext.getContext().put("message", "上傳成功!");
- String path = request.getContextPath();
- String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
- String picturePath = basePath +"uploadImages"+"/"+imageFileName;
- model.setImage(picturePath);
- //request.setAttribute("uploadsuccess", imageFileName);
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }else{
- model.setImage("");
- }
- //增加微博的時候將當前登錄人對應的頭像信息路徑加入到微博表中(此時model中已經存在userName)
- String picturePathString = getiWeiboService().selectPicturePath(model);
- model.setAvatar(picturePathString);
- getiWeiboService().insertWeibo(model);
- try {
- response.sendRedirect("jsp/weibo/index.jsp");
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- /*****************以上爲上傳部分*******************************/
- 後臺將圖片的地址保存到數據庫中
- //先從數據庫中將所有數據讀出來,放入到request中
- request.setAttribute("weibotest", list);
- 前臺使用OGNL語言讀取出圖片地址,並且顯示圖片
- <s:iterator value="#request.weibotest" var="user">
- <s:property value="#user.getContent()"/>
- <img src ='<s:property value ="#user.getImage()" />' width="200"> //顯示圖片
- </s:iterator>
struts2文件和圖片上傳-最完整的
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.