您好,登录后才能下订单哦!
密码登录
登录注册
点击 登录注册 即表示同意《亿速云用户服务条款》
这期内容当中小编将会给大家带来有关怎么在Java中使用Struts2上传图片,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文章希望大家可以有所收获。
创建视图文件
让我们开始创建需要浏览和上传选定的文件的视图。因此,让我们创建一个带有简单的 HTML 上传表单的 index.jsp,它允许用户上传文件:(表单的编码类型设置为multipart/form-data)
<%-- Created by IntelliJ IDEA. User: yzjxiaoyue Date: 2017/7/28 Time: 19:11 To change this template use File | Settings | File Templates. --%> <%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <%@ taglib prefix="s" uri="/struts-tags"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <title>File Upload</title> </head> <body> <form action="upload" method="post" enctype="multipart/form-data"> <label for="myFile">Upload your file</label> <input type="file" name="myFile" id="myFile"/> <input type="submit" value="Upload"/> </form> </body> </html>
之后创建success.jsp页面:
<%-- Created by IntelliJ IDEA. User: yzjxiaoyue Date: 2017/7/28 Time: 19:14 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html; charset=UTF-8" %> <%@ taglib prefix="s" uri="/struts-tags" %> <html> <head> <title>File Upload Success</title> </head> <body> You have successfully uploaded <s:property value="myFileFileName"/> </body> </html>
创建error.jsp页面
<%-- Created by IntelliJ IDEA. User: yzjxiaoyue Date: 2017/7/28 Time: 20:05 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html; charset=UTF-8" %> <%@ taglib prefix="s" uri="/struts-tags" %> <html> <head> <title>File Upload Error</title> </head> <body> There has been an error in uploading the file. </body> </html>
创建 action 类
接下来让我们创建一个称为 uploadFile.java 的 Java 类,它负责上传文件,并且把这个文件存储在一个安全的位置:
package com.action; import com.opensymphony.xwork2.ActionSupport; import org.apache.commons.io.FileUtils; import java.io.File; import java.io.IOException; public class uploadFile extends ActionSupport{ private File myFile; public File getMyFile() { return myFile; } public void setMyFile(File myFile) { this.myFile = myFile; } private String myFileContentType; private String myFileFileName; private String destPath; public String execute() { /* Copy file to a safe location */ destPath = "E:\\Program Files\\apache-tomcat-9.0.0\\apache-tomcat-9.0.0.M22\\work\\"; try{ System.out.println("Src File name: " + myFile); System.out.println("Dst File name: " + myFileFileName); File destFile = new File(destPath, myFileFileName); FileUtils.copyFile(myFile, destFile); }catch(IOException e){ e.printStackTrace(); return ERROR; } return SUCCESS; } public String getMyFileContentType() { return myFileContentType; } public void setMyFileContentType(String myFileContentType) { this.myFileContentType = myFileContentType; } public String getMyFileFileName() { return myFileFileName; } public void setMyFileFileName(String myFileFileName) { this.myFileFileName = myFileFileName; } }
配置文件
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" "http://struts.apache.org/dtds/struts-2.3.dtd"> <struts> <constant name="struts.devMode" value="true"/> <constant name="struts.multipart.maxSize" value="10000000"/> <constant name="struts.multipart.saveDir" value="/tmp"/> <constant name="struts.custom.i18n.resources" value="struts"></constant> <package name="default" namespace="/" extends="struts-default"> <action name="upload" class="com.action.uploadFile"> <!--<interceptor-ref name="basicStack"/>--> <interceptor-ref name="defaultStack"/> <interceptor-ref name="fileUpload"> <param name="allowedTypes">image/jpeg,image/jpg,image/gif</param> </interceptor-ref> <result name="success">/success.jsp</result> <result name="error">/error.jsp</result> </action> </package> </struts>
Java是一门面向对象编程语言,可以编写桌面应用程序、Web应用程序、分布式系统和嵌入式系统应用程序。
上述就是小编为大家分享的怎么在Java中使用Struts2上传图片了,如果刚好有类似的疑惑,不妨参照上述分析进行理解。如果想知道更多相关知识,欢迎关注亿速云行业资讯频道。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。