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
}
}
Uploadify 是一个比较坑的东西,得用servlet, web.xml里配置 然后servlet接收<servlet>
<servlet-name>upload</servlet-name>
<servlet-class>com.action.upload</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>upload</servlet-name>
<url-pattern>/upload/FileUploadServlet</url-pattern>
</servlet-mapping>
servlet里代码
public void doGetAndPost(HttpServletRequest request,
HttpServletResponse response){
try {
request.setCharacterEncoding("utf-8")
} catch (UnsupportedEncodingException e) {
logger.error("后台添加图片,request设置编码符失败! {}",e.getMessage())
}
DiskFileItemFactory fac = new DiskFileItemFactory()
ServletFileUpload upload = new ServletFileUpload(fac)
upload.setHeaderEncoding("utf-8")
List fileList = null
try {
fileList = upload.parseRequest(request)
} catch (FileUploadException ex) {
ex.printStackTrace()
return
}
String imageName = null
Iterator<FileItem>it = fileList.iterator()
while (it.hasNext()) {
FileItem item = it.next()
if (!item.isFormField()) {
Random r = new Random()
int rannum = (int) (r.nextDouble() * (9999 - 1000 + 1)) + 1000
imageName=getNowStrDate() + rannum
FileBean fileBean = new FileBean()
fileBean.setFileName(item.getName())
fileBean.setFileExtension(item.getName().substring(item.getName().indexOf(".")+1))
FileBean flbn
try {
//文件服务器处理上传图片
flbn = FastDFSUtil.upload(fileBean, item.getInputStream())
} catch (IOException e) {
logger.info("添加图片,上传文件服务器失败!",e)
}
}
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)