Struts2 업로드 다운로드 및 페이지 이미지 렌더링 기능
11203 단어 struts2업로드페이지 렌더링 이미지
<?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>
<constant name="struts.devMode" value="true" />
<constant name="struts.custom.i18n.resources" value="messages" />
<constant name="struts.configuration.xml.reload" value="true" />
<constant name="struts.enable.SlashesInActionNames" value="true" />
<constant name="struts.ognl.allowStaticMethodAccess" value="true" />
<constant name="struts.multipart.maxSize" value="30480000" />
<constant name="struts.ui.theme" value="simple" />
<package name="main" extends="struts-default">
<default-interceptor-ref name="paramsPrepareParamsStack" />
<action name="*-*" class="com.chou.web.PlayerAction" method="{2}">
<interceptor-ref name="token">
<param name="includeMethods">save</param>
</interceptor-ref>
<interceptor-ref name="paramsPrepareParamsStack">
<param name="fileUpload.allowedTypes">image/bmp,image/png,image/gif,image/jpeg,image/jpg,image/x-png,image/pjpeg</param>
<param name="fileUpload.allowedExtensions">.jpg,.gif,.jpeg,.bmp,.png</param>
<param name="fileUpload.maximumSize">20971520</param>
</interceptor-ref>
<result name="input">player-load.jsp</result>
<result name="invalid.token">player-load.jsp</result>
<result name="success">{1}-{2}.jsp</result>
<result name="list" type="redirect">player-list</result>
<result name="download" type="stream">
<param name="contentType">${fileContentType}</param>
<param name="contentLength">${fileContentLength}</param>
<param name="contentDisposition">attachment;filename="${fileFileName}"</param>
<param name="inputName">fileStream</param>
<param name="bufferSize">4096</param>
</result>
</action>
</package>
</struts>
public class PlayerAction extends AbstractBaseAction<Player> {
private static final long serialVersionUID = -3068068486865209475L;
PlayerDao playerDao = new PlayerDao();
/**
*
*/
private File file;
private String fileFileName;
private InputStream fileStream;
private String fileContentType;
private int fileContentLength;
public String list() throws Exception {
return SUCCESS;
}
public void prepareSave() throws Exception {
Player player = getModel();
if (getId() != null) {
player = playerDao.loadPlayer(getId());
}
setModel(player);
}
public String save() throws Exception {
// System.out.println(fileContentType);
// System.out.println(fileFileName);
Player player = getModel();
if (file != null) {//
FileInputStream fis = null;
byte[] buf = new byte[(int) file.length()];
try {
fis = new FileInputStream(file);
fis.read(buf);
player.setImage(buf);
} catch (IOException e) {
e.printStackTrace();
addActionError(" 。");
LOG.error(" 。", e, new String[0]);
return ERROR;
} finally {
if (fis != null) {
try {
fis.close();
} catch (IOException e) {
LOG.error(" FileInputStream 。", e, new String[0]);
}
}
}
} else if (file == null && getId() != null) {// ,
player.setImage(playerDao.loadPlayer(getId()).getImage());
}
if (player != null) {
player.setCreatetime(new Date());
playerDao.saveOrUpdate(player);
return "list";
}
return INPUT;
}
public void prepareLoad() throws Exception {
Player player = getModel();
if (getId() != null) {
player = playerDao.loadPlayer(getId());
player.setImage(null);
} else {
player.setBirthday(new Date());
player.setContract(" ");
}
setModel(player);
}
public String load() throws Exception {
return SUCCESS;
}
public void prepareDownload() throws Exception {
Player player = getModel();
if (getId() != null) {
player = playerDao.loadPlayer(getId());
}
setModel(player);
}
public String download() throws Exception {
if (getId() != null) {
try {
Player player = getModel();
setFileContentType("image/jpg");
setFileContentLength(player.getImage().length);
setFileFileName(new String(player.getName().getBytes(),
"iso-8859-1") + ".jpg");
setFileStream(new ByteArrayInputStream(player.getImage()));
} catch (Exception e) {
LOG.error(" 。", new String[0]);
}
}
return "download";
}
//
public String showImage() throws Exception {
byte[] bigImgByte = null;
OutputStream out = null;
HttpServletResponse response = ServletActionContext.getResponse();
if (getId() == null) {
response.setHeader("Content-disposition", "attachment; filename="
+ "no.jpg");
response.setContentType("image/jpeg");
out = response.getOutputStream();
out.write(bigImgByte);
out.flush();
return NONE;
}
Player player = playerDao.loadPlayer(getId());
if (player.getImage() == null) {
return NONE;
}
bigImgByte = player.getImage();
try {
response.setHeader("Content-disposition", "attachment; filename="
+ player.getName().trim().replace(" ", "") + ".jpg");
response.setContentType("image/jpeg");
out = response.getOutputStream();
out.write(bigImgByte);
out.flush();
} catch (Exception e) {
System.out.println(" !!");
} finally {
try {
if (null != out)
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return NONE;
}
/**
* getter....setter
*/
}
<body>
<div align="center"><h1> </h1></div>
<s:if test="hasErrors()">
<s:fielderror/>
<s:actionerror/>
</s:if>
<s:form id="sForm" action="player-save" enctype="multipart/form-data" method="post" onsubmit="return verifyUpload()">
<s:hidden name="id" />
<s:token/>
<table align="center" border="1">
<tr>
<th> </th>
<td><s:textfield name="name" maxlength="20" required="true"/></td>
<th> </th>
<td>
</td>
</tr>
<tr>
<th> </th>
<td><s:textfield name="high" maxlength="5"/></td>
<th> </th>
<td><s:textfield name="weight" maxlength="5"/></td>
</tr>
<tr>
<th> </th>
<td><s:textfield name="team" maxlength="30"/></td>
<th> </th>
<td><s:select name="place" list="placeKinds" listKey="key" listValue="value"/></td>
</tr>
<tr>
<th> </th>
<td><s:textfield name="no" maxlength="2"/></td>
<th> </th>
<td><s:textfield name="draft" maxlength="30"/></td>
</tr>
<tr>
<th> </th>
<td><s:textfield name="nationality" maxlength="10"/></td>
<th> </th>
<td><s:textfield name="school" maxlength="50"/></td>
</tr>
<tr>
<th> </th>
<td><s:textfield name="salary" maxlength="10"/></td>
<th> </th>
<td>
<s:file name="file" />
</td>
</tr>
<tr>
<td>
<img style="cursor:pointer" alt="<s:property value="name"/>" src="<s:url action="player-showImage" ><s:param name="id"><s:property value="id"/></s:param></s:url>">
</td>
<th> </th>
<td colspan="2"><s:textarea name="contract" cols="30" rows="4"/></td>
</tr>
<tr>
<td colspan="4" align="center">
<s:reset value=" "/>
<s:submit value=" "/>
</td>
</tr>
</table>
</s:form>
</body>
struts.messages.error.content.type.not.allowed= : "{1}"
struts.messages.error.file.extension.not.allowed= , "{1}"
struts.messages.error.file.too.large= “{1}” (20M)
struts.messages.invalid.token=
xwork.default.invalid.fieldvalue= , 、 "{0}"
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
apache struts2 취약점 검증이번에는 보안 캠프의 과제였던 apache struts2의 취약성에 대해 실제로 손을 움직여 실행해 보고 싶습니다. 환경 VirtualBox에서 브리지 어댑터 사용 호스트:macOS 10.12 게스트:ubuntu 1...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.