Java는 struts2를 예로 들어 사진 업로드를 어떻게 하는지 소개합니다.

6259 단어 java사진 업로드
전체적으로 말하면 사진 업로드는 두 가지 방식이 있는데 하나는 사진 파일을 데이터베이스에 쓰는 것이고, 다른 하나는 서버 파일 디렉터리에 저장하는 것이다.데이터베이스에 쓴 이미지 파일은 이진 흐름의 형식으로 전환해야 한다. 데이터베이스 공간을 차지하는 비교는 소량의 이미지 저장에 적합하다. 예를 들어 시스템의 일부 작은 아이콘은 데이터베이스에 쓰는 장점이 비교적 안전하고 사용자가 부주의로 삭제하기 쉽다는 것이다.
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든 액션으로 할 수 있어요.
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>
위 내용은 여러분께 소개해 드린 Javastruts2에서 어떻게 사진이 업로드되는 모든 내용을 실현할 수 있는지 여러분의 사랑을 부탁드립니다.

좋은 웹페이지 즐겨찾기