일반적으로 이미지 파일을 데이터베이스에 작성하는 두 가지 방법이 있으며 다른 하나는 서버 파일 디렉토리에 저장하는 것입니다. 데이터베이스에 작성된 이미지 파일은 데이터베이스 공간을 차지하고 소수의 이미지를 저장하는 데 적합한 이진 스트림 형식으로 변환해야합니다. 비교적 안전하고 사용자는 쉽게 피할 수 없습니다.
struts2에서 구현 (이미지 업로드 촬영)
1. FileUpload.jsp 코드 목록은 다음과 같습니다.
<%@ page language = "java"import = "java.util.*"pageencoding = "utf-8"%> <%@ taglib prefix = "s"uri = "/struts-tags"%> <html> < Head> <title> struts2의 fileUplaoddemo </title> </head> <body> <s : form action = "fileUpload"method = "post"enctepe = "multipart/form-data"name space = "/"> <s : 파일 이름 = "myFile"label = "myFile"> </s : 파일> <s : textfield name = "caption"label = "caption"> </s : textfield> <s : label = "제출 ">/s : 제출> </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; 테두리 : Solid 1px #cccccc; text-align : center"> <img src = "uploadima ges/<s : 속성 값 = "ImageFileName"/> "/> <br/> <s : 속성 값 ="캡션 "/> </div>/body> </html>
3. FileUploadAction.java의 코드 목록은 다음과 같습니다.
패키지 com.chris; import java.io.*; import java.util.date; import org.apache.struts2.servletactioncontext; import com.opensymphony.xwork2.active onsupport; public class fileUploadaction은 ActionSupport {private static final long serialversioniid = 572146812454L; private static final int buffer_size = 16 * 1024; // 파일이 업로드 될 때 <s : 파일 //은 myFile, myFileContentType, myFileFilename에 바인딩됩니다. Time Lename의 개인 파일 MyFile; // 업로드 파일 유형 개인 문자열 filename; setMyFileContentType (String contenttype) {System.out .println ( "파일 유형 :" + contentType); public void setmyFileFileName (String filename) {System.out.println ( "파일 이름 :" + filename ; this .filename = filename;} public void setmyfile (file myfile) {this .myfile = myfile;} public String getImageFilename () {ret imageFilename;} public string getCaption () {return 캡션;} public void setcaption (문자열 캡션. ) {this .caption = caption;} private static void copy (file src, file dst) {try {inputStream in = null; outputStream out = null; in = new bufferedInputStream (src), bu ffer_size); out = new bufferedOutputStream (new FileOutputStream (dst), buffer_size); byte [] buffer = new Byte [buffer_size]; while (in.read (buffer)> 0) {out.write (buffer);}} 최종 {if (if (if). null! = in) {in.close ();} if (null! = out) {out.close ();}}} catch (예외 e) {e.printstacktrace ();}} private static string getextention (string filename) {int pos = filename.lastindexof ( "."); return filename.substring (pos);}@attradepublic string execute () {imagefilename = new date (). gettime (); 파일 이미지 파일 = 새 파일 (servletactionContext.getServletContext (). getRealPath ( "uploadImages") + "/" + imageFilename); copy (myFile, imageFile); return success;}}참고 : 현재 조치 구현의 편의를 위해서만 ActionSupport가 상속되고 Overrider Execute () 메소드는 다음과 같습니다.
struts2의 모든 pojo는 행동으로 사용될 수 있습니다.
4.struts.xml 목록은 다음과 같습니다.
<? xml version = "1.0"encoding = "utf-8"?> <! doctype struts public "-// Apache Software Foundation // dtd struts configuration 2.0 // en" "http : // strut s.apache.org /dtds/struts-2.0.dtd "> <struts> <package name ="example ""namespace = "/"extends = "struts-default"> <action name = "fileUpload"> <interceptor -ref name = "FileUploadStack" /> <atured>/showupload.jsp </resent> </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/xml/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 </fi lter -name> <filter-class> org.apache.struts2.dispatcher.filterdispatcher </filter-class> </filter> <filter-mapping> <filter-name> struts2 </filter-na me> <url-pattern >/ *</url-pattern> </필터-맵핑> <welcome-file-list> <welcome-file> index.jsp </welcome-file> </welcome-file-list> </web-app>
위의 내용은 Java Struts2에서 이미지를 업로드하는 방법의 모든 내용입니다.