|
解凍するファイル
C:\Users\(ユーザ名)\Downloads\struts-7.0.0-all.zip 解凍するとできるフォルダ C:\Users\(ユーザ名)\Downloads\struts-7.0.0-all\struts-7.0.0 |
|
解凍するファイル
C:\Users\(ユーザ名)\Downloads\struts-7.0.0-all\struts-7.0.0\apps\struts2-rest-showcase-7.0.0.war 解凍するとできるフォルダ C:\Users\(ユーザ名)\Downloads\struts-7.0.0-all\struts-7.0.0\apps\struts2-rest-showcase-7.0.0 |
|
コピー元
C:\Users\(ユーザ名)\Downloads\struts-7.0.0-all\struts-7.0.0\apps\struts2-rest-showcase-7.0.0\WEB-INF\lib\* コピー先 C:\pleiades\2024-12\workspace\(プロジェクト名)\src\main\webapp\WEB-INF\lib (重要)コピー先から以下のjarファイルを削除 struts2-rest-plugin-7.0.0.jar |
|
package test5.sample.action; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import org.apache.struts2.ActionSupport; import org.apache.struts2.action.UploadedFilesAware; import org.apache.struts2.dispatcher.multipart.UploadedFile; /** * <code>Allows upload a file</code> */ public class Upload extends ActionSupport implements UploadedFilesAware { private UploadedFile[] upload; private String[] uploadFileName; private String[] uploadContentType; private String tmpPath = "c:/tmp/"; public String execute() throws Exception { if (upload != null && upload.length > 0) { if (! new File(tmpPath).exists()) { if (! new File(tmpPath).mkdir()) { throw new Exception("mkdir failed."); } } int i; for (i = 0; i<upload.length; i++) { String fileFullPath = upload[i].getAbsolutePath(); //System.out.println(fileFullPath); FileInputStream fileIn = new FileInputStream(fileFullPath); FileOutputStream fileOut = new FileOutputStream(tmpPath + uploadFileName[i]); // byte型の配列を宣言 byte[] buf = new byte[256]; int len; // ファイルの終わりまで読み込む while((len = fileIn.read(buf)) != -1){ fileOut.write(buf); } //ファイルに内容を書き込む fileOut.flush(); //ファイルの終了処理 fileOut.close(); fileIn.close(); } } return INPUT; } public File[] getUpload() { return upload; } public void setUpload(File[] upload) throws Exception { this.upload = upload; } public String[] getUploadFileName() { return uploadFileName; } public void setUploadFileName(String[] uploadFileName) { this.uploadFileName = uploadFileName; } public String[] getUploadContentType() { return uploadContentType; } public void setUploadContentType(String[] uploadContentType) { this.uploadContentType = uploadContentType; } @Override public void withUploadedFiles(List upload = new UploadedFile[uploadedFiles.size()]; uploadFileName = new String[uploadedFiles.size()]; uploadContentType = new String[uploadedFiles.size()]; int i; for (i = 0; i<uploadedFiles.size(); i++) { this.upload[i] = uploadedFiles.get(i); this.uploadFileName[i] = upload[i].getOriginalName(); this.uploadContentType[i] = upload[i].getContentType(); // this.Name = upload[i].getName(); // this.inputName = upload[i].getInputName(); } } } |
|
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib prefix="s" uri="/struts-tags" %> <!DOCTYPE html> <html> <head> <title>File upload</title> </head> <body> <s:form action="upload" method="post" enctype="multipart/form-data"> <s:file name="upload1"/> <s:file name="upload2"/> <s:file name="upload3"/> <s:submit/> </s:form> <%--<s:iterator value="upload" var="u"> <s:property value="u"/><br/> </s:iterator> --%> <s:iterator value="uploadContentType" var="ct"> <s:property value="ct"/><br/> </s:iterator> <s:iterator value="uploadFileName" var="fn"> <s:property value="fn"/><br/> </s:iterator> </body> </html> |
|
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN" "https://struts.apache.org/dtds/struts-2.5.dtd"> <struts> <constant name="struts.enable.DynamicMethodInvocation" value="false"/> <constant name="struts.devMode" value="true"/> <constant name="struts.allowlist.packageNames" value="test5.sample" /> <constant name="struts.multipart.maxSize" value="10000000"/><!-- デフォルト2MB --> <package name="default" namespace="/" extends="struts-default"> <default-action-ref name="index"/> <action name="index"> <result type="redirectAction"> <param name="actionName">upload</param> </result> </action> <action name="upload" class="test5.sample.action.Upload"> <interceptor-ref name="actionFileUpload"> <param name="maximumSize">5000000</param> <param name="allowedTypes">image/jpeg,image/gif</param> </interceptor-ref> <result name="input">/upload.jsp</result> </action> </package> <!-- Add addition packages and configuration here. --> </struts> |
|
<?xml version="1.0" encoding="UTF-8"?> <!-- /* * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. */ --> <Configuration> <Appenders> <Console name="STDOUT" target="SYSTEM_OUT"> <PatternLayout pattern="%d %-5p [%t] %C{2} (%F:%L) - %m%n"/> </Console> </Appenders> <Loggers> <Logger name="org.apache.struts2" level="info"/> <Logger name="test5" level="debug"/> <Root level="warn"> <AppenderRef ref="STDOUT"/> </Root> </Loggers> </Configuration> |
|
<?xml version="1.0" encoding="UTF-8"?> <web-app> <display-name>test5</display-name> <filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>default.html</welcome-file> <welcome-file>default.jsp</welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file>index</welcome-file> </welcome-file-list> </web-app> |
|
struts.messages.error.file.too.large=Too Large! {0} {1} {2} {3} {4} struts.messages.error.content.type.not.allowed=not allowed Content-Type {0} {1} {2} {3} |
|
<!DOCTYPE html> <html> <head> <META HTTP-EQUIV="Refresh" CONTENT="0;URL=upload.action"> </head> <body> <p>Loading ...</p> </body> </html> |