Struts2学习笔记 - 第003天

文件上传

例子:用户照片上传
jsp

            

action

    private File[] photo;
    private String[] photoFileName;
    private String[] photoContentType;

        // set方法

    public String register() {
        if (photo != null) {
            List 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配置

        
            
            
                
                
            
        
        
        

转换器

例子:字符串经纬度自动转换成对象
经纬高类

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配置

        
            /error.html
        
    
        
            
        

你可能感兴趣的:(Struts2学习笔记 - 第003天)