java文件并发上传代码 java 上传多个文件

用Java的三大框架实现文件的上传下载,求代码啊,最好是分为action,service,servpackage cn.itcast.struts2.demo1;
import java.io.File;
import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
/**
* 完成文件上传 (不是解析上传内容,因为上传内容 由fileUpload拦截器负责解析)
*
* @author seawind
*
*/
public class UploadAction extends ActionSupport {
// 接收上传内容
// input type="file" name="upload" /
private File upload; // 这里变量名 和 页面表单元素 name 属性一致
private String uploadContentType;
private String uploadFileName;
public void setUpload(File upload) {
this.upload = upload;
}
public void setUploadContentType(String uploadContentType) {
this.uploadContentType = uploadContentType;
}
public void setUploadFileName(String uploadFileName) {
this.uploadFileName = uploadFileName;
}
@Override
public String execute() throws Exception {
if (upload == null) { // 通过xml配置 required校验器 完成校验
// 没有上传文件
return NONE;
}
// 将上传文件 保存到服务器端
// 源文件 upload
// 目标文件
File destFile = new File(ServletActionContext.getServletContext()
.getRealPath("/upload") + "/" + uploadFileName);
// 文件复制 使用commons-io包 提供 工具类
FileUtils.copyFile(upload, destFile);
return NONE;
}
}
多文件上传
package cn.itcast.struts2.demo1;
import java.io.File;
import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
/**
* 支持多文件上传
*
* @author seawind
*
*/
public class MultiUploadAction extends ActionSupport {
// 接收多文件上传参数,提供数组接收就可以了
private File[] upload;
private String[] uploadContentType;
private String[] uploadFileName;
public void setUpload(File[] upload) {
this.upload = upload;
}
public void setUploadContentType(String[] uploadContentType) {
this.uploadContentType = uploadContentType;
}
public void setUploadFileName(String[] uploadFileName) {
this.uploadFileName = uploadFileName;
}
@Override
public String execute() throws Exception {
for (int i = 0; iupload.length; i++) {
// 循环完成上传
File srcFile = upload[i];
String filename = uploadFileName[i];
// 定义目标文件
File destFile = new File(ServletActionContext.getServletContext()
.getRealPath("/upload" + "/" + filename));
FileUtils.copyFile(srcFile, destFile);
}
return NONE;
}
}
java 文件上传的代码,尽量详细一点 。。。// 这是java文件并发上传代码我写的一个方法java文件并发上传代码,里面只需要传两个参数就OK了 , 在任何地方调用此方法都可以文件上传
/**
* 上传文件
* @param file待上传的文件
* @param storePath待存储的路径(该路径还包括文件名)
*/
public void uploadFormFile(FormFile file,String storePath)throws Exception{
// 开始上传
InputStream is =null;
OutputStream os =null;
try {
is = file.getInputStream();
os = new FileOutputStream(storePath);
int bytes = 0;
byte[] buffer = new byte[8192];
while ((bytes = is.read(buffer, 0, 8192)) != -1) {
os.write(buffer, 0, bytes);
}
os.close();
is.close();
} catch (Exception e) {
throw e;
}
finally{
if(os!=null){
try{
os.close();

推荐阅读