文件上传解决方法 -------收集
来源:互联网 发布:烘焙软件哪个好 编辑:程序博客网 时间:2024/06/09 16:58
方法一(文件流方式):
private static final int BUFFER_SIZE = 16 * 1024;
//得到保存路径
String basePath=request.getSession().getServletContext().getRealPath("/");//得到项目路径
String dir=basePath+"images/"+fileDate+"/";
//拷贝上传文件到目标文件---即上传文件
private static void copy(File src, File dst) {
try {
InputStream in = null;
OutputStream out = null;
try {
if (!dst.exists() ||dst== null) { //判断指定路径dir是否存在,不存在则创建路径
dst.mkdirs();
}
in = new BufferedInputStream(new FileInputStream(src),
BUFFER_SIZE);
out = new BufferedOutputStream(new FileOutputStream(dst),
BUFFER_SIZE);
byte[] buffer = new byte[BUFFER_SIZE];
while (in.read(buffer) > 0) {
out.write(buffer);
}
} finally {
if (null != in) {
in.close();
}
if (null != out) {
out.close();
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
方法二(struts2中通FileUtils去上传-----单个文件):
===========页面中的form表单===========
<form action="${pageContext.request.contextPath}/control/employee/list_execute.action" enctype="multipart/form-data" method="post">
文件:<input type="file" name="image">
<input type="submit" value="上传"/>
</form>
============对应struts2的action==============
import java.io.File;
import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionContext;
public class HelloWorldAction {
private File image;// 页面上对应文件的名字
private String imageFileName;//这样可以得到struts2默认文件名(页面文件名+FileName)
public String getImageFileName() {
return imageFileName;
}
public void setImageFileName(String imageFileName) {
this.imageFileName = imageFileName;
}
public File getImage() {
return image;
}
public void setImage(File image) {
this.image = image;
}
public String execute() throws Exception{//上传文件方法
String realpath = ServletActionContext.getServletContext().getRealPath("/images");//得到images文件的绝对路径
System.out.println(realpath);
if(image!=null){
File savefile = new File(new File(realpath), imageFileName);
if(!savefile.getParentFile().exists()) savefile.getParentFile().mkdirs();
FileUtils.copyFile(image, savefile);
ActionContext.getContext().put("message", "上传成功");
}
return "success";
}
}
注: <constant name="struts.multipart.maxSize" value="10701096"/> 在struts.xml中配置上传的最大文件,这里设置时10M
方法三(struts2中通FileUtils去上传-----多个文件):
===========页面中的form表单===========
<form action="${pageContext.request.contextPath}/control/employee/list_execute.action" enctype="multipart/form-data" method="post">
文件1:<input type="file" name="image"><br/>
文件2:<input type="file" name="image"><br/>
文件3:<input type="file" name="image"><br/>
<input type="submit" value="上传"/>
</form>
============对应struts2的action==============
import java.io.File;
import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionContext;
public class HelloWorldAction {
private File[] image;// 页面上对应文件的名字---------------------和单个文件不同点是定义为数组
private String[] imageFileName;//这样可以得到struts2默认文件名(页面文件名+FileName)-----------------和单个文件不同点是定义为数组
public File[] getImage() {
return image;
}
public void setImage(File[] image) {
this.image = image;
}
public String[] getImageFileName() {
return imageFileName;
}
public void setImageFileName(String[] imageFileName) {
this.imageFileName = imageFileName;
}
public String execute() throws Exception{
String realpath = ServletActionContext.getServletContext().getRealPath("/images");
System.out.println(realpath);
if(image!=null){
File savedir = new File(realpath);
if(!savedir.exists()) savedir.mkdirs();
for(int i = 0 ; i<image.length ; i++){
File savefile = new File(savedir, imageFileName[i]);
FileUtils.copyFile(image[i], savefile);
}
ActionContext.getContext().put("message", "上传成功");
}
return "success";
}
}
- 文件上传解决方法 -------收集
- 文件上传代码收集
- JQuery 文件上传收集
- JQuery 文件上传收集
- asp.net上传文件收集
- 文件上传S3代码收集
- 异步上传文件解决方法
- 关于多文件上传解决方法
- 上传文件代码(网上收集)
- struts2文件上传相关资料收集整合
- AspNet上传文件的几个控件(收集)
- AspNet上传文件的几个控件(收集)
- 本机上传文件的代码收集
- asp.net上传大文件解决方法
- ASP上传大文件的解决方法
- Vsftpd 不能上传文件的错误解决方法
- grails ckeditor上传文件,中文名称乱码解决方法
- .net多文件上传的解决方法
- 山寨文化
- HQL函数汇总
- Oracle中如何计算时间差
- C++运行原理的汇编代码分析 一
- window.open打开窗口时要使窗口去掉菜单栏、工具栏、标题栏,达到窗口最大化
- 文件上传解决方法 -------收集
- 什么是程序员的优秀品质
- 重定向和转发有什么区别
- 北大齐教授健康讲座笔录
- 解决长字符串换行的问题
- Emacs下变更ediff打开比较窗口的方式
- 应用日常生活对ERP的理解
- oracle遍历树语句
- 小故事三则