锘??xml version="1.0" encoding="utf-8" standalone="yes"?> org.springframework.dao.InvalidDataAccessApiUsag 闂錛?span style="font-family: 瀹?huà)浣? color: #0055ff; font-size: 14px">鍙妯″紡涓?FlushMode.NEVER/MANUAL)鍐欐搷浣滀笉琚厑璁革細(xì)鎶婁綘鐨凷ession鏀規(guī)垚FlushMode.COMMIT/AUTO鎴栬呮竻闄や簨鍔″畾涔変腑鐨剅eadOnly鏍囪銆?/span> 閿欒鍘熷洜: web.xml閰嶇疆閲屾坊鍔?/span> 濡傛灉鍦ㄤ氦緇檚pring 綆$悊鐨勬儏鍐典笅錛屽湪beans.xml 閲岀殑閰嶇疆
package com.hyl.action; import java.io.File; public class UploadAction extends ActionSupport { public String upload() throws IOException { InputStream is = new FileInputStream(file); File serverFile = new File(path, date + fileExtenName); OutputStream os = new FileOutputStream(serverFile); byte[] b = new byte[1024]; int length = 0; while ((length = is.read(b)) > 0) { } public File getFile() { public void setFile(File file) { public String getFileFileName() { public void setFileFileName(String fileFileName) { public String getFileContentType() { public void setFileContentType(String fileContentType) { public String getUserName() { public void setUserName(String userName) { public String getUserPwd() { public void setUserPwd(String userPwd) { } package com.hyl.action; import java.io.InputStream; public class DownLoadAction extends ActionSupport { public InputStream getDownLoad() throws UnsupportedEncodingException { public String execute() throws Exception { return super.execute(); public String getPicName() { public void setPicName(String picName) { } package com.hyl.util; import java.text.SimpleDateFormat; public class DateUtil { public static String dateTimeChange(Date source) { public static String shortDate(Date aDate) { public static String nowDate() { public static String mailDate(Date aDate) { public static String dateParser(Date aDate) { public static Date parser(String strDate) { public static Date parser(String strDate, String formatter) { public static String parser(Date date, String formatter) { public static Date addMonth(Date myDate, int amount) { public static Date addDay(Date myDate, int amount) { public static Date addMinute(Date myDate, int amount) { if (amount > 60) { int hour = (int) amount / 60; if (hour * 60 > amount) { cal.add(Calendar.HOUR_OF_DAY, -hour); } else if (hour * 60 < amount) { minute = amount - hour * 60; } else { } else { cal.add(Calendar.MINUTE, -amount); public static Date addYear(Date myDate, int amount) { public static int getWeekDay(Date myDate) { public static int getConvertWeekDay(Date myDate) { public static int getTimeFromDate(Date myDate) { public static long getDaysBetweenDate(Date startDate, Date endDate) { } public static String strDateTime(String str) { public static String strDotDateTime(String str) { public static String bakDateTime(String str) { public static String strShortDateTime(String str) { } public static int getBetweenDayNumber(String dateA, String dateB) { public static void main(String[] args) { struts.messages.error.file.too.large=\u6587\u4EF6\u8FC7\u5927 <?xml version="1.0" encoding="UTF-8" ?> </package> </struts>
http://fd.itedu-g.cn/login.php
]]>
Write operations are not allowed in read-only mode 鍙妯″紡涓?FlushMode.NEVER/MANUAL)鍐欐搷浣滀笉鍏?/h2>
瑙e喅鏂規(guī)硶:
<filter>
</filter>
<filter-mapping>
</filter-mapping>
]]>
<body>
<s:fielderror></s:fielderror>
<s:form action="uploadUploadAction" enctype="multipart/form-data"
theme="simple">
鐢ㄦ埛鍚嶏細(xì)<s:textfield name="userName" />
<br />
瀵嗙爜錛?<s:textfield name="userPwd" />
<br />
<input type="file" name="file" />
<br />
<s:submit value="鎻愪氦"></s:submit>
</s:form>
<br />
涓嬭澆<a href="DownLoadAction">寮濮?gif</a>
</body>
UploadAction
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import org.apache.struts2.ServletActionContext;
import com.hyl.util.DateUtil;
import com.opensymphony.xwork2.ActionSupport;
private File file;
private String fileFileName;
private String fileContentType;
private String userName;
private String userPwd;
String path = ServletActionContext.getRequest().getRealPath("/upload");
// System.out.println(path);
String date = DateUtil.mailDate(new java.util.Date());
// 鎴彇鐨勬枃浠舵墿灞曞悕
String fileExtenName = fileFileName
.substring(fileFileName.indexOf('.'));
// System.out.println("鎴彇鐨勬枃浠舵墿灞曞悕"+fileName);
os.write(b);
os.close();
is.close();
return SUCCESS;
}
return file;
}
this.file = file;
}
return fileFileName;
}
this.fileFileName = fileFileName;
}
return fileContentType;
}
this.fileContentType = fileContentType;
}
return userName;
}
this.userName = userName;
}
return userPwd;
}
this.userPwd = userPwd;
}
DownLoadAction
import java.io.UnsupportedEncodingException;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
//涓嬮潰浠ヤ腑鏂囧悕鏂囦歡瀹炰緥
//姝ゅ鏂囦歡鍚嶇О鐢辯敤鎴瘋緭鍏ワ紝姝ゅ涔熸槸鍔ㄦ佷紶鍙傜殑榪囩▼
private String picName = "寮濮?gif";
//姝ゅ鍋氫竴涓腑闂村彉閲忥紝褰撻噸鏂扮紪鐮佸悗灝辨棤娉曡瘑鍒腑鏂囧悕浜?br /> String rourseName=picName;
//灝嗘簮鏂囦歡鐨勪腑鏂囧悕閲嶆柊緙栫爜錛岀洰鐨勫艱Struts鐨勯厤緗枃浠朵腑鑳借瘑鍒埌錛屽憟鐜扮粰鐢ㄦ埛鐪?br /> picName=new String(picName.getBytes(),"iso-8859-1");
System.out.println("/upload/"+rourseName);
return ServletActionContext.getServletContext().getResourceAsStream(
"/upload/"+rourseName);
}
}
return picName;
}
this.picName = picName;
}
DateUtil
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String changeTime = format.format(source);
return changeTime;
}
SimpleDateFormat formatter = new SimpleDateFormat("MM/dd/yyyy");
return formatter.format(aDate);
}
String iDate = "";
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
String str = formatter.format(new Date());
String[] date = str.split("-");
if (date.length >= 3) {
iDate = date[0] + "/" + date[1] + "/" + date[2] + "";
} else {
iDate = str;
}
return iDate;
}
SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMddHHmmssSSS");
return formatter.format(aDate);
}
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
return formatter.format(aDate);
}
;
strDate = strDate.replace("/", "-");
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
try {
return sdf.parse(strDate);
} catch (Exception e) {
return null;
}
}
SimpleDateFormat sdf = new SimpleDateFormat(formatter);
try {
return sdf.parse(strDate);
} catch (Exception e) {
return null;
}
}
SimpleDateFormat sdf = new SimpleDateFormat(formatter);
try {
return sdf.format(date);
} catch (Exception e) {
return null;
}
}
GregorianCalendar cal = new GregorianCalendar();
cal.setTime(myDate);
boolean isEndDayOfMonth_old = cal
.getActualMaximum(GregorianCalendar.DAY_OF_MONTH) == cal
.get(GregorianCalendar.DAY_OF_MONTH);
cal.add(GregorianCalendar.MONTH, amount);
boolean isEndDayOfMonth_new = cal
.getActualMaximum(GregorianCalendar.DAY_OF_MONTH) == cal
.get(GregorianCalendar.DAY_OF_MONTH);
if (isEndDayOfMonth_old && !isEndDayOfMonth_new) {
cal.set(GregorianCalendar.DATE, cal
.getActualMaximum(GregorianCalendar.DAY_OF_MONTH));
}
return cal.getTime();
}
Calendar cal = Calendar.getInstance();
cal.setTime(myDate);
cal.add(Calendar.DAY_OF_MONTH, amount);
return cal.getTime();
}
Calendar cal = Calendar.getInstance();
cal.setTime(myDate);
int minute = 0;
amount = -(amount);
minute = hour * 60 - amount;
cal.add(Calendar.MINUTE, minute);
cal.add(Calendar.HOUR_OF_DAY, -hour);
cal.add(Calendar.MINUTE, -minute);
cal.add(Calendar.HOUR_OF_DAY, -hour);
}
}
return cal.getTime();
}
GregorianCalendar cal = new GregorianCalendar();
cal.setTime(myDate);
boolean isEndDayOfMonth_old = cal
.getActualMaximum(GregorianCalendar.DAY_OF_MONTH) == cal
.get(GregorianCalendar.DAY_OF_MONTH);
cal.add(GregorianCalendar.YEAR, amount);
boolean isEndDayOfMonth_new = cal
.getActualMaximum(GregorianCalendar.DAY_OF_MONTH) == cal
.get(GregorianCalendar.DAY_OF_MONTH);
if (isEndDayOfMonth_old && !isEndDayOfMonth_new) {
cal.set(GregorianCalendar.DATE, cal
.getActualMaximum(GregorianCalendar.DAY_OF_MONTH));
}
return cal.getTime();
}
GregorianCalendar cal = new GregorianCalendar();
cal.setTime(myDate);
return cal.get(GregorianCalendar.DAY_OF_WEEK);
}
int day = getWeekDay(myDate);
int result = day - 1;
if (result == 0)
result = 7;
return result;
}
SimpleDateFormat sdf = new SimpleDateFormat("hhmmss");
int result = Integer.parseInt(sdf.format(myDate));
return result;
}
Calendar cal = Calendar.getInstance();
cal.setTime(startDate);
cal.set(Calendar.HOUR, 0);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MILLISECOND, 0);
startDate = cal.getTime();
cal.setTime(endDate);
cal.set(Calendar.HOUR, 0);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MILLISECOND, 0);
return (cal.getTime().getTime() - startDate.getTime()) / 86400000;
String idate = "";
if (str != null) {
String[] date = str.split("-");
if (date.length >= 3) {
idate = date[0] + "." + date[1] + "." + date[2];
} else {
idate = str;
}
}
return idate;
}
String idate = "";
if (str != null) {
String data0 = null;
String[] date = str.split("-");
if (date.length >= 3) {
if (date[0] != null) {
data0 = date[0].substring(2, 4);
}
idate = data0 + "." + date[1] + "." + date[2];
} else {
idate = str;
}
}
return idate;
}
String idate = "";
if (str != null) {
int l1 = str.indexOf(".");
String d1 = str.substring(0, l1);
String s1 = str.substring(l1 + 1);
int l2 = s1.indexOf(".");
String d2 = s1.substring(0, l2);
String d3 = s1.substring(l2 + 1);
idate = d1 + "-" + d2 + "-" + d3;
}
return idate;
}
String idate = "";
if (str != null) {
String[] date = str.split("-");
if (date.length >= 3) {
idate = date[0] + "." + date[1] + "." + date[2];
} else {
idate = str;
}
if (idate != null && idate.length() > 9) {
idate = idate.substring(0, 10);
}
return idate;
}
long dayNumber = 0;
long DAY = 24L * 60L * 60L * 1000L;
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
try {
java.util.Date d1 = df.parse(dateA);
java.util.Date d2 = df.parse(dateB);
dayNumber = (d2.getTime() - d1.getTime()) / DAY;
} catch (Exception e) {
e.printStackTrace();
}
return (int) dayNumber;
}
System.out.println(nowDate());
}
}
messageFile.properties
struts.messages.error.content.type.not.allowed=\u6587\u4EF6\u7C7B\u578B\u4E0D\u4E00\u81F4
struts.xml
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "<struts>
<!--鍔犺澆Struts鐨勬秷鎭祫婧愭枃浠?nbsp; value閲岄潰鎸囧畾messageFile.properties鐨勬枃浠跺悕瀛楋紝涓嶅啓鎵╁睍鍚嶅瓧-->
<constant name="struts.custom.i18n.resources" value="messageFile"></constant>
<package name="hyl" extends="struts-default">
<!-- 涓婁紶鐨凙ction -->
<action name="*UploadAction" class="com.hyl.action.UploadAction"
method="{1}">
<result name="success">/ok.jsp</result>
<result name="input">/index.jsp</result>
<interceptor-ref name="fileUpload">
<param name="allowedTypes">image/jpeg</param>
<param name="maximumSize">102400</param>
</interceptor-ref>
<!-- 榪欎釜榛樿鐨勬嫤鎴櫒蹇呴』鏀懼湪鑷畾涔夋嫤鎴櫒鐨勪笅闈紝鍚﹀垯鑷畾涔夋嫤鎴櫒涓嶈兘鍛楄皟鐢?-->
<interceptor-ref name="defaultStack"></interceptor-ref>
</action>
<!-- 涓嬭澆鐨凙ction -->
<action name="DownLoadAction" class="com.hyl.action.DownLoadAction">
<result name="success" type="stream">
<!-- 榪欓噷鏄劇ず鐨勬寚瀹氳繑鍥炵被鍨嬶紝鍍忚繖縐嶈兘琚祻瑙堝櫒璇嗗埆鐨勭被鍨嬩細(xì)鐩存帴鏄劇ず鍑烘潵錛?br /> 濡傛灉鏄叾浠栫被鍨嬶紝浼?xì)浠ヤ笅铦插舰寮忓嚭鐜板Q?br /> 濡傛灉鍘繪帀榪欎釜鏍囩錛屼細(xì)鍚敤榛樿鐨則ext/plain
-->
<param name="contentType">image/jpeg</param>
<!-- 姝ゅ鐨勬柟娉曟槸Action涓殑getDownLoad鏂規(guī)硶錛?-->
<param name="inputName">downLoad</param>
<param name="contentDisposition">filename="${picName}"</param>
<param name="bufferSize">1024</param>
</result>
</action>
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts2.ServletActionContext;
import com.hyl.action.UserInfoAction;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.Interceptor;
public class UserLoginInterceptor implements Interceptor {
public void destroy() {
System.out.println("鎴戞槸鐧誨綍鎷︽埅鍣ㄩ攢姣?);
}
public void init() {
System.out.println("鎴戞槸鐧誨綍楠岃瘉鎷︽埅鍣ㄥ垵濮嬪寲");
}
public String intercept(ActionInvocation invocation) throws Exception {
System.out.println("鎴戞鍦ㄨ繘琛岀櫥褰曟嫤鎴姛鑳?);
String result = "";
// 鍒ゆ柇褰撳墠瑕佽皟鐢ㄧ殑Action瀹炰緥錛堝璞★級(jí) 鏄惁鏄?鐧誨綍楠岃瘉鐨凙ction
if (invocation.getAction() instanceof UserInfoAction) {
result = invocation.invoke();
} else {
//褰撳墠瑕佽皟鐢ㄧ殑涓嶆槸鐧誨綍鐨凙ction
//浠巗ession涓彇鍊鹼紝鍒ゆ柇鏄惁鏄┖錛堢櫥褰曪級(jí)
//鐧誨綍鐨凙ction涓皢鐧誨綍鐢ㄦ埛淇℃伅瀛樺偍鍒皊ession涓?key鍊兼槸褰撳墠鐨剆essionID
HttpServletRequest request = ServletActionContext.getRequest();
Map session = invocation.getInvocationContext().getSession();
if (session.get(request.getSession().getId()) != null) {
result = invocation.invoke();
} else {
//session涓鏋滀笉瀛樺湪灝辮煩鍒扮櫥褰曢〉闈?br /> return "error";
}
}
return result;
}
}
Struts閰嶇疆鏂囦歡鍐呭
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "<struts>
<package name="userInfo" namespace="/userInfo" extends="struts-default">
<interceptors>
<interceptor name="userInter" class="com.hyl.inter.UserLoginInterceptor"></interceptor>
<interceptor-stack name="myStack">
<!--鍔犺澆Struts榛樿鎷︽埅鍣?->
<interceptor-ref name="defaultStack"></interceptor-ref>
<interceptor-ref name="userInter"></interceptor-ref>
</interceptor-stack>
</interceptors>
<default-interceptor-ref name="myStack"></default-interceptor-ref>
<action name="*userInfoMan" class="com.hyl.action.UserInfoAction" method="{1}">
<result name="success1" type="redirect">/menu.jsp</result>
<result name="error" type="redirect">/user_login.jsp</result>
</action>
</package>
</struts>