文件上传
例子:用户照片上传
jsp
<form id="form" action="User_register.do" method="post" enctype="multipart/form-data">
<div class="item">
<input type="file" name="photo" size="28" multiple>
</div>
</form>
action
private File[] photo;
private String[] photoFileName;
private String[] photoContentType;
// set方法
public String register() {
if (photo != null) {
List<String> filenames = new ArrayList<>();
for (int i = 0; i < photo.length; i++) {
if (canAccept(photoContentType[i])) {
String filename = getRandomFileName(photoFileName[i]);
filenames.add(filename);
String fullPath = getPath() + "/" + filename;
try (InputStream in = new FileInputStream(photo[i])) {
Files.copy(in, Paths.get(fullPath));
}
catch (Exception e) {
e.printStackTrace();
return "failed";
}
}
}
user.setPhotoFilename(filenames.toArray(new String[0]));
}
return "success";
}
private boolean canAccept(String contentType) {
return contentType.equals("image/jpeg") ||
contentType.equals("image/png") ||
contentType.equals("image/gif");
}
private String getSuffix(String currentFileName) {
return currentFileName.lastIndexOf(".") > 0 ?
currentFileName.substring(currentFileName.lastIndexOf(".")) : "";
}
private String getRandomFileName(String currentFileName) {
return UUID.randomUUID().toString() + getSuffix(currentFileName);
}
private String getPath() {
return ServletActionContext.getServletContext()
.getRealPath(path);
}
文件copy
try (InputStream in = new FileInputStream(photo[i]);
OutputStream out = new FileOutputStream(fullPath)) {
// Files.copy(in, Paths.get(fullPath));
byte[] buf = new byte[1024];
while (in.read(buf) != -1) {
out.write(buf);
}
拦截器
例子:算出执行action花费的时间
pref拦截器
public class PerfInterceptor extends AbstractInterceptor {
@Override
public String intercept(ActionInvocation invocation)
throws Exception {
long start = System.currentTimeMillis();
String result = invocation.invoke();
long end = System.currentTimeMillis();
String name = invocation.getInvocationContext().getName();
System.out.println(name + "执行时间: " + (end - start) + "ms");
return result;
}
}
struts.xml配置
<interceptors>
<interceptor name="perf" class="org.mobiletrain.interceptor.PerfInterceptor" />
<interceptor-stack name="myStack">
<interceptor-ref name="perf" />
<interceptor-ref name="defaultStack" />
</interceptor-stack>
</interceptors>
<default-interceptor-ref name="myStack" />
转换器
例子:字符串经纬度自动转换成对象
经纬高类
public class GeoLocation {
private double latitude;
private double longitude;
private double altitude;
}
转换器 继承StrutsTypeConverter 或者 DefaultConverter
public class GeoLocationConverter extends StrutsTypeConverter {
@Override
public Object convertFromString(Map context, String[] values, Class toClass) {
String locationStr = values[0];
String[] strs = locationStr.split("#");
if (strs.length == 3) {
GeoLocation location = new GeoLocation();
location.setLatitude(Double.parseDouble(strs[0]));
location.setLongitude(Double.parseDouble(strs[1]));
location.setAltitude(Double.parseDouble(strs[2]));
return location;
}
return null;
}
@Override
public String convertToString(Map context, Object o) {
GeoLocation location = (GeoLocation) o;
return location.getLatitude() + "#" + location.getLongitude() +
"#" + location.getAltitude();
}
}
配置自己的转换器 创建xwork-conversion.properties文件(格式必须)
com.kygo.entity.GeoLocation=com.kygo.converter.GeoLocationConverter
错误页面struts.xml配置
<global-results>
<result name="error">/error.html</result>
</global-results>
<global-exception-mappings>
<exception-mapping result="error"
exception="java.lang.Exception" />
</global-exception-mappings>