struts2上传文件,原理,方式等总结.

struts2文件上传底层用的还是commons-fileupload

底层原理:

public class UploadServlet extends HttpServlet
{
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException
{
DiskFileItemFactory factory = new DiskFileItemFactory();

String path = req.getRealPath("/upload");

factory.setRepository(new File(path));
factory.setSizeThreshold(1024 * 1024);

ServletFileUpload upload = new ServletFileUpload(factory);

try
{
List<FileItem> list = (List<FileItem>)upload.parseRequest(req);

for(FileItem item : list)
{
String name = item.getFieldName();

if(item.isFormField())
{
String value = item.getString();

System.out.println(name + "=" + value);

req.setAttribute(name, value);
}
else
{
String value = item.getName();

int start = value.lastIndexOf("\\");
String fileName = value.substring(start + 1);

req.setAttribute(name, fileName);

item.write(new File(path, fileName));
//
// OutputStream os = new FileOutputStream(new File(path, fileName));
//
// InputStream is = item.getInputStream();
//
// byte[] buffer = new byte[400];
//
// int length = 0;
//
// while((length = is.read(buffer)) != -1)
// {
// os.write(buffer, 0, length);
// }
//
// is.close();
// os.close();
}
}

}
catch(Exception ex)
{
ex.printStackTrace();
}

req.getRequestDispatcher("fileUploadResult.jsp").forward(req, resp);
}
}

Struts2在进行文件上传操作时,实际上是通过两个步骤实现的:
1) 首先将客户端上传的文件保存到struts.multipart.saveDir键所指定的目录中,如果该键所对应的目录不存在,那么就保存到javax.servlet.context.tempdir环境变量所指定的目录中。
2) Action中所定义的File类型的成员变量file实际上指向的是临时目录中的临时文件,然后在服务器端通过IO的方式将临时文件写入到指定的服务器端目录中。

struts.multipart.saveDir是在哪里指定的呢?在org.apache.struts2的default.properties中进行指定的,如下:

struts.multipart.saveDir=
struts.multipart.maxSize=2097152

如果想要覆盖这个文件的东西怎么办?自己编写struts.properties文件,然后修改里面的属性的值即可.

用struts2进行文件的上传如下:

public class UploadAction extends ActionSupport
{
private String username;

private File file;//当流程转到Action里了,那么file就已经指向真正的文件了.

private String fileFileName;

private String fileContentType;//注意最后两个的成员变量的命名是有规则的.


public String getUsername()
{
return username;
}


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


public File getFile()
{
return file;
}


public void setFile(File file)
{
this.file = file;
}


public String getFileFileName()
{
return fileFileName;
}


public void setFileFileName(String fileFileName)
{
this.fileFileName = fileFileName;
}


public String getFileContentType()
{
return fileContentType;
}


public void setFileContentType(String fileContentType)
{
this.fileContentType = fileContentType;
}


@Override
public String execute() throws Exception
{
String root = ServletActionContext.getRequest().getRealPath("/upload");

InputStream is = new FileInputStream(file);

System.out.println("path: " + file.getAbsolutePath());

System.out.println("file: " + file.getName());

System.out.println("fileFileName: " + fileFileName);

File destFile = new File(root, fileFileName);

OutputStream os = new FileOutputStream(destFile);

byte[] buffer = new byte[400];

int length = 0;

while(-1 != (length = is.read(buffer)))
{
os.write(buffer, 0, length);

Thread.sleep(1000);
}

is.close();
os.close();

return SUCCESS;

}
}
批量如何进行上传:

public class UploadAction2 extends ActionSupport
{
private String username;

private List<File> file;

private List<String> fileFileName;

private List<String> fileContentType;


public String getUsername()
{
return username;
}


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


public List<File> getFile()
{
return file;
}


public void setFile(List<File> file)
{
this.file = file;
}


public List<String> getFileFileName()
{
return fileFileName;
}


public void setFileFileName(List<String> fileFileName)
{
this.fileFileName = fileFileName;
}


public List<String> getFileContentType()
{
return fileContentType;
}


public void setFileContentType(List<String> fileContentType)
{
this.fileContentType = fileContentType;
}

@Override
public String execute() throws Exception
{
for(int i = 0; i < file.size(); i++)
{
InputStream is = new FileInputStream(file.get(i));

String root = ServletActionContext.getRequest().getRealPath("/upload");

File destFile = new File(root, fileFileName.get(i));

OutputStream os = new FileOutputStream(destFile);

byte[] buffer = new byte[400];

int length = 0;

while(-1 != (length = is.read(buffer)))
{
os.write(buffer, 0, length);
}

is.close();
os.close();
}

return SUCCESS;
}

}

文件上传也是通过interceptor来进行拦截的,fileupload这个拦截器.并且已经加入到默认的拦截器栈里面去了.通过这个拦截器可以设定一些参数.如上传的最大字节数等,但不起作用,用default.properties里面的类似配置进行配置.
struts2的常量配置.<constant name="">的配置.如:<constant name="struts.multipart.maxSize" value="1048576000"></constant>,跟struts.properties里面的配置谁的优先级高呢?是struts.properties高.但是实际开发中要么在struts.properties配,要么在struts.xml中进行配,本身是同一回事来的.

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

yjsuge

你的鼓励是我最大的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值