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 = 0i <upload.lengthi++) {
// 循环完成上传
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()
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)