码迷,mamicode.com
首页 > Web开发 > 详细

Struts中的文件上传

时间:2019-09-03 22:25:56      阅读:116      评论:0      收藏:0      [点我收藏+]

标签:nbsp   mit   dir   BMI   password   需要   value   dac   类型   

Struts中的表单

<s:form action="/upload" enctype="multipart/form-data">
<s:textfield label="用户名" name="username"/>
<s:textfield label="密码" name="password"/>
<s:file label="照片" name="photo"/>
<s:submit value="上传"/>
</s:form>

注意:使用struts中的标签需要导入<%@ taglib prefix="s" uri="/struts-tags" %>

动作类

package com.lzw.web.action;
import java.io.File;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;

public class UploadAction extends ActionSupport{
private String username;
private String password;
private File photo;
private String photoContentType;//文件类型
private String photoFileName;//文件名称

public String upload() {
System.out.println(username);
System.out.println(password);
System.out.println(photoContentType);
System.out.println(photoFileName);
System.out.println(photo);

//1设置文件保存目录
String dir = ServletActionContext.getServletContext().getRealPath("/WEB-INF/upload") + "/" + username;

//创建文件路径
File f = new File(dir);
if(f.exists() == false) {
      f.mkdirs();
}
String fileSavePath = dir + "/" + photoFileName;

photo.renameTo(new File(fileSavePath));//保存文件
      return NONE;
}

public void setUsername(String username) {
      this.username = username;
}

public void setPassword(String password) {
      this.password = password;
}

public void setPhoto(File photo) {
      this.photo = photo;
 }

public void setPhotoContentType(String photoContentType) {
      this.photoContentType = photoContentType;
}

public void setPhotoFileName(String photoFileName) {
      this.photoFileName = photoFileName;
 }
}

Struts中的文件上传

标签:nbsp   mit   dir   BMI   password   需要   value   dac   类型   

原文地址:https://www.cnblogs.com/xiaowenwen/p/11455871.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!