// 这是我写的一个方法,里面只需要传两个参数就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();
os=null;
}catch(Exception e1){
;
}
}
if(is!=null){
try{
is.close();
is=null;
}catch(Exception e1){
;
}
}
}
}
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;
/**
* 完成文件上传 (不是解析上传内容,因为上传内容 由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; i upload.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代码实现文件上传
FormFile file=manform.getFile();
String newfileName = null;
String newpathname=null;
String fileAddre="/numUp";
try {
InputStream stream = file.getInputStream();// 把文件读入
String filePath = request.getRealPath(fileAddre);//取系统当前路径
File file1 = new File(filePath);//添加了自动创建目录的功能
((File) file1).mkdir();
newfileName = System.currentTimeMillis()
+ file.getFileName().substring(
file.getFileName().lastIndexOf('.'));
ByteArrayOutputStream baos = new ByteArrayOutputStream();
OutputStream bos = new FileOutputStream(filePath + "/"
+ newfileName);
newpathname=filePath+"/"+newfileName;
System.out.println(newpathname);
// 建立一个上传文件的输出流
System.out.println(filePath+"/"+file.getFileName());
int bytesRead = 0;
byte[] buffer = new byte[8192];
while ((bytesRead = stream.read(buffer, 0, 8192)) != -1) {
bos.write(buffer, 0, bytesRead);// 将文件写入服务器
}
bos.close();
stream.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}