1. 스트럿 파일 업로드 아이디어
이전에 FileUpload 구성 요소에 대해서도 이야기했습니다. 강력한 기능이 있지만 작업은 복잡하고 복잡합니다. 이번에는 파일 업로드도 Strust에서 지원되며 FileUpload는 포장되어 사용하기에 훨씬 편리합니다.
여기에 태그와 클래스가 있습니다.
<html : file property = "actionform의 속성 이름에 해당">
이 태그를 사용하여 파일을 업로드하지만 내용을 받으려면 org.apache.struts.upload.formfile에 의존해야합니다.
인터페이스가 완료되었습니다.
2. 실현
sh.jsp :
<%@ page language = "java"import = "java.util.*"pageencoding = "utf-8"%> <%@ taglib uri = "http://jakarta.apache.org/stags-bean"prefix = "bean"%> <%@ taglib uri = "http://jakarta.apache.org/struts/tags-html"prefix = "html"%> <%@ taglib uri = "http://jakarta.apache.org/struts/tags-logic"prefix = "Logic"%> <! duttype html "-// w3c" HTML 4.01 전환 // en "> <html> <head> <title> sh.jsp </title> <meta http-equiv ="content-type "content ="text/html; <html : file property = "photo"> </html : file> <html : value = "upload"> </html : 제출> </html : form> </body> </html>
uploadform.java :
패키지 com.zyy.struts.form; import org.apache.struts.action.actionform; import org.apache.struts.upload.formfile; public class uploadform actionform {private formfile 사진; public formfile getPhoto () {반환 사진; } public void setphoto (formfile photo) {this.photo = photo; }} iptimestamp.java :
패키지 com.zyy.util; import java.text.simpledateformat; import java.util.date; import java.util.random; public class iptimestamp {private simpledateformat sdf = null; 개인 문자열 ip = null; public iptimestamp () {} public iptimestamp (String IP) {this.ip = ip; } public String getiptimerand () {StringBuffer buf = new StringBuffer (); if (this.ip! = null) {문자열 s [] = this.ip.split ( "//."); for (int i = 0; i <s.length; i ++) {buf.append (this.addzero (s [i], 3)); }} buf.append (this.getTimeStamp ()); 랜덤 r = 새로운 랜덤 (); for (int i = 0; i <3; i ++) {buf.append (r.nextint (10)); } return buf.toString (); } public String getDate () {this.sdf = new SimpledateFormat ( "yyyy-mm-dd hh : mm : ss.sss"); reture this.sdf.format (new date ()); } public String getTimestamp () {this.sdf = new SimpledateFormat ( "yyyymmddhhmmssss"); reture this.sdf.format (new date ()); } private String addzero (String str, int len) {StringBuffer s = new StringBuffer (); S.append (str); while (s.length () <len) {s.insert (0, "0"); } return s.toString (); } public static void main (String args []) {System.out.println (new iptimestamp ( "192.168.1.1"). getiptimerand ()); }}uploadaction.java :
패키지 com.zyy.struts.action; import java.io.file; import java.io.fileoutputStream; import java.io.inputStream; import java.io.outputstream; import javax.servlet.http.httpservletrequest; import javax.servlet.http.http.httprongronge; org.apache.struts.action.action; import org.apache.struts.action.actionform; import org.apache.structs.action.actionforward; import org.apache.structs.action.actionmapping; import com.zyy.structs.form.uploadform; import com.zyy.util.iptimestamp; public matport actions actions actuctor actionforwarter actionforwarter actionforforcte action offorforforcate actuction extenct. 양식, httpservletrequest request, httpservletresponse 응답) 예외 {ubloadform ubloadform = (uploadform) 양식; iptimestamp ips = 새로운 iptimestamp (request.getRemoteaddr ()); // 파일 이름 문자열 filename = ips.getiptimerand () + "." + uploadform.getphoto (). getFilename (). split ( "//.") [uploadform.getphoto (). getFilename (). split ( "//"). 길이 -1]; // 출력 경로 파일 outFile = 새 파일 (super.getServlet (). getServletContext () .getRealPath ( "/") + "Upload" + file.separator + filename); // 사진이 저장된 폴더 파일 파일 = 새 파일 (super.getServlet (). getServletContext () .getRealPath ( "/") + "업로드"); if (! file.exists ()) {file.mkdir (); } inputStream input = uploadform.getphoto (). getInputStream (); outputStream output = 새 FileOutputStream (Outfile); 바이트 데이터 [] = 새로운 바이트 [1024]; int temp = 0; while ((temp = input.read (data, 0, 1024))! = -1) {output.write (data); } output.close (); input.close (); 널 리턴; }}struts-config.xml :
<? xml version = "1.0"encoding = "iso-8859-1"?> <! doctype struts-config public "-// Apache Software Foundation // dtd struts 구성 1.2 // en" "http://jakarta.apchache.org/struts/dtdds/struts-config_1_2. <form-beans> <form-bean name = "upload form"type = "com.zyy.struts.form.uploadform"> </form-bean> </form-beans> <global-exceptions> </global-exceptions> <global-forwards> </global-forwards> <action-mappings> <action> <action> 이름 = "uploadform"scope = "request"type = "com.zyy.struts.action.uploadaction"> </action> </action-mappings> <message-resources parameter = "resource.messagerEsources"/> </struts-config>
내가 저장하는 것은 super.getServlet (). getServletContext (). getRealPath ( "/")+ "업로드"입니다.
이것은 업로드 폴더의 가상 디렉토리의 실제 경로입니다.
Struts에서 파일 업로드의 원칙은 FileUpload와 동일하지만 Struts는 포장되어 있으므로 FileUpload 구성 요소를 사용하는 것보다 사용하는 것이 훨씬 편리합니다.
위는이 기사의 모든 내용입니다. 모든 사람의 학습에 도움이되기를 바랍니다. 모든 사람이 wulin.com을 더 지원하기를 바랍니다.