package action;public void setUploadContentTypes(List<String> uploadContentType) {
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;
import org.apache.commons.io.IOUtils;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
@SuppressWarnings("serial")
public class uoloadAction extends ActionSupport{
private List<File> upload ;
private List<String> uploadFileName;
private List<String> uploadContentType ;
public String doUpload() throws IOException{
for(int i=0 ; i<upload.size();i++){
System.out.println(uploadFileName.get(i));
String savePath = ServletActionContext.getServletContext().getRealPath("/upload/"+uploadFileName.get(i));
FileInputStream fis = new FileInputStream(upload.get(i));
FileOutputStream fos = new FileOutputStream(savePath);
IOUtils.copy(fis, fos);
fos.flush();
fis.close();
fos.close();
}
return SUCCESS;
}
public List<File> getUpload() {
return upload;
}
public void setUploads(List<File> upload) {
this.upload = upload;
}
public List<String> getUploadFileName() {
return uploadFileName;
}
public void setUploadFileName(List<String> uploadFileName) {
this.uploadFileName = uploadFileName;
}
public List<String> getUploadContentType() {
return uploadContentType;
}
this.uploadContentType = uploadContentType;
}
}
[/code]
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<constant name="struts.i18n.encoding" value="UTF-8" />
<constant name="struts.ui.theme" value="simple" />
<constant name="struts.enable.DynamicMethodInvocation" value="true" />
<package name="upload" namespace="/" extends="struts-default">
<action name="doUpload" class="action.uoloadAction" method="doUpload">
<result>/singleSuccess.jsp</result>
<result name="input">/error.jsp</result>
</action>
</package>
</struts>
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>上传文件</title>
<script type="text/javascript">
function addFile(){
var btn = document.getElementById("btn");
var newChild = document.createElement("input");
newChild.setAttribute("type", "file");
newChild.setAttribute("name", "upload");
btn.parentNode.appendChild(newChild);
btn.parentNode.appendChild(document.createElement("br"));
}
</script>
</head>
<body>
<form action="doUpload" method="post" enctype="multipart/form-data"> <!-- enctype="multipart/form-data" 这是上传文件的特定用法 -->
<input type="file" name="upload">
<input type="button" value="继续添加" onclick="addFile()" id="btn">
<input type="submit" value="上传图片"><br>
</form>
</body>
</html>
上传2个图片后 在上传的文件夹中显示的是 一个图片
1个图片名是 3.jpg, 7.jpg(我上传的2个文件名)
这是什么意思??
求高人给解!!!给解啊
------解决思路----------------------
首先要确定,页面的值能不能自动赋值给 Action的属性 List
可以debug一下,看下list中有多少元素
文件上传毕竟和页面值传递不一样
------解决思路----------------------
String savePath = ServletActionContext.getServletContext().getRealPath("/upload/"+uploadFileName.get(i));//+uploadFileName.get(i)这个有问题吧?刚开始文件肯定是不存在的吧?
FileInputStream fis = new FileInputStream(upload.get(i));
FileOutputStream fos = new FileOutputStream(savePath);//保存的时候加上uploadFileName.get(i)才对。
------解决思路----------------------
可以把控制台输出结果发上来: System.out.println(uploadFileName.get(i));,这样也可以缩小范围
------解决思路----------------------
执行了你的程序,报错:FIleNotFoundException
不知道哪里设置,不同于楼主
指定位置没有任何文件
因此不使用 IO 流,改用如下代码,成功执行:
public class UploadAction extends ActionSupport {
/**
*
*/
private static final long serialVersionUID = 3258740595336733237L;
private File[] upload;
private String[] uploadFileName;
public String execute() throws IOException {
for (int i = 0; i < upload.length; i++) {
System.out.println(uploadFileName[i]);
String savePath = new String("D:/upload/" + uploadFileName[i]);
System.out.println(savePath);
FileUtils.copyFile(upload[i], new File(savePath));
}
return SUCCESS;
}
/**
* @return the upload
*/
public File[] getUpload() {
return upload;
}
/**
* @param upload the upload to set
*/
public void setUpload(File[] upload) {
this.upload = upload;
}
/**
* @return the uploadFileName
*/
public String[] getUploadFileName() {
return uploadFileName;
}
/**
* @param uploadFileName the uploadFileName to set
*/
public void setUploadFileName(String[] uploadFileName) {
this.uploadFileName = uploadFileName;
}
}
------解决思路----------------------
多文件上传和单文件上传是不一样的,你应该去看看struts的多文件上传,肯定是后面上传的文件把你前面上传的覆盖了,如果是多文件上传的话,那么前台应该是一个数组形式,后台也有相应的改变的。
http://www.cnblogs.com/linjiqin/archive/2011/03/21/1990688.html
------解决思路----------------------
doUpload方法改下,万一项目下没有upload目录是需要创建的
public String doUpload() throws IOException {
for (int i = 0; i < upload.size(); i++) {
System.out.println(uploadFileName.get(i));
String uploadPath= ServletActionContext.getServletContext().getRealPath("/upload/");
File file = new File( uploadPath);
if(!file.exists()){
file.mkdir();
}
String savePath =uploadPath +File.separator+ uploadFileName.get(i);
FileInputStream fis = new FileInputStream(upload.get(i));
FileOutputStream fos = new FileOutputStream(savePath);
IOUtils.copy(fis, fos);
fos.flush();
fis.close();
fos.close();
}
return SUCCESS;
}