程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> 關於JAVA >> Java以struts2為例引見若何完成圖片上傳

Java以struts2為例引見若何完成圖片上傳

編輯:關於JAVA

Java以struts2為例引見若何完成圖片上傳。本站提示廣大學習愛好者:(Java以struts2為例引見若何完成圖片上傳)文章只能為提供參考,不一定能成為您想要的結果。以下是Java以struts2為例引見若何完成圖片上傳正文


總的說圖片上傳有兩種方法,一種是把圖片文件寫到數據庫中,另外一種是存到辦事器文件目次中。寫到數據庫中的圖片文件須要轉換成二進制流的格局,占用數據庫空間比擬,合適大批圖片的存儲,好比說,體系中某些小圖標,寫到數據庫中的長處是比擬平安,不輕易被用戶不當心刪除。

在struts2中完成(以圖片上傳為例)

1.FileUpload.jsp代碼清單以下:

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
<title>The FileUplaodDemo In Struts2</title>
</head>
<body>
<s:form action="fileUpload" method="post" enctype="multipart/form-data" namespace="/">
<s:file name="myFile" label="MyFile" ></s:file>
<s:textfield name="caption" label="Caption"></s:textfield>
<s:submit label="提交"></s:submit>
</s:form>
</body>
</html>

2.ShowUpload.jsp的功效清單以下:

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
<title>ShowUpload</title>
</head>
<body>
<div style ="padding: 3px; border: solid 1px #cccccc; text-align: center" >
<img src ="UploadImages/<s:property value ="imageFileName"/> "/>
<br />
<s:property value ="caption"/>
</div >
</body>
</html>

3.FileUploadAction.java的代碼清單以下 :

package com.chris;
import java.io.*;
import java.util.Date;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class FileUploadAction extends ActionSupport{
private static final long serialVersionUID = 572146812454l ;
private static final int BUFFER_SIZE = 16 * 1024 ;
//留意,文件上傳時<s:file/>同時與myFile,myFileContentType,myFileFileName綁定
//所以同時要供給myFileContentType,myFileFileName的set辦法
private File myFile; //上傳文件
private String contentType;//上傳文件類型
private String fileName; //上傳文件名
private String imageFileName;
private String caption;//文件解釋,與頁面屬性綁定
public void setMyFileContentType(String contentType) {
System.out.println("文件類型 : " + contentType);
this .contentType = contentType;
}
public void setMyFileFileName(String fileName) {
System.out.println("文件稱號 : " + fileName);
this .fileName = fileName;
}
public void setMyFile(File myFile) {
this .myFile = myFile;
}
public String getImageFileName() {
return imageFileName;
}
public String getCaption() {
return caption;
}
public void setCaption(String caption) {
this .caption = caption;
}
private static void copy(File src, File dst) {
try {
InputStream in = null ;
OutputStream out = null ;
try {
in = new BufferedInputStream( new FileInputStream(src), BUFFER_SIZE);
out = new BufferedOutputStream( new FileOutputStream(dst), BUFFER_SIZE);
byte [] buffer = new byte [BUFFER_SIZE];
while (in.read(buffer) > 0 ) {
out.write(buffer);
}
} finally {
if ( null != in) {
in.close();
}
if ( null != out) {
out.close();
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
private static String getExtention(String fileName) {
int pos = fileName.lastIndexOf(".");
return fileName.substring(pos);
}
@Override
public String execute() {
imageFileName = new Date().getTime() + getExtention(fileName);
File imageFile = new File(ServletActionContext.getServletContext().getRealPath("UploadImages" ) + "/" + imageFileName);
copy(myFile, imageFile);
return SUCCESS;
}
}

注:此時僅為便利完成Action所以繼續ActionSupport,並Overrider execute()辦法

在struts2中任何一個POJO都可以作為Action

4.struts.xml清單以下:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<package name="example" namespace="/" extends="struts-default">
<action name="fileUpload" class="com.chris.FileUploadAction">
<interceptor-ref name="fileUploadStack"/>
<result>/ShowUpload.jsp</result>
</action>
</package>
</struts>

5.web.xml清單以下:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
 http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<filter >
<filter-name > struts-cleanup </filter-name >
 <filter-class >
org.apache.struts2.dispatcher.ActionContextCleanUp
</filter-class >
 </filter >
 <filter-mapping >
<filter-name > struts-cleanup </filter-name >
 <url-pattern > /* </url-pattern >
 </filter-mapping >
 <filter>
 <filter-name>struts2</filter-name>
 <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
 </filter>
 <filter-mapping>
<filter-name>struts2</filter-name>
 <url-pattern>/*</url-pattern>
 </filter-mapping>
 <welcome-file-list>
 <welcome-file>Index.jsp</welcome-file>
 </welcome-file-list>
</web-app>

以上內容是小編給年夜家引見的Java struts2中若何完成圖片上傳的全體內容,願望年夜家愛好。

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved