页面显示:a.jsp
________
|________|浏览 上传
_________
|________|浏览 上传
。
。点上传后,经过处理,将再次回到a.jsp,此时的显示如下:***.doc x(x为删除图标)
***.jpg x(x为删除图标)
如果点x后,变成
__________
|_________|浏览 上传
*****.jpg x(x为删除图标)请问大家有做过类似的上传吗,或者能否告知整个思路呢
________
|________|浏览 上传
_________
|________|浏览 上传
。
。点上传后,经过处理,将再次回到a.jsp,此时的显示如下:***.doc x(x为删除图标)
***.jpg x(x为删除图标)
如果点x后,变成
__________
|_________|浏览 上传
*****.jpg x(x为删除图标)请问大家有做过类似的上传吗,或者能否告知整个思路呢
如果会上传一个,那么多个对应的就一个数组 【】
<%@ page language="java" contentType="text/html; charset=gb2312"
pageEncoding="gb2312"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!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=gb2312">
<title>Insert title here</title><script type="text/javascript">
function addMore()
{
var td = document.getElementById("addfile");
var br = document.createElement("br");
var input = document.createElement("input");
var button = document.createElement("input");
input.type = "file";
input.name = "file1";
button.type = "button";
button.value = "remove";
button.onclick = function()
{
td.removeChild(br);
td.removeChild(input);
td.removeChild(button);
}
td.appendChild(br);
td.appendChild(input);
td.appendChild(button);
}
</script>
</head>
<body>
<s:form action="upload" enctype="multipart/form-data" method="post" theme="simple">
<table border="1px">
<tr>
<td>
userName:
</td>
<td>
<s:textfield label="username" name="username"></s:textfield>
</td>
</tr>
<tr>
<td>
password:
</td>
<td>
<s:password label="password" name="password"></s:password>
</td>
</tr>
<tr>
<td>
file :
</td>
<td id="addfile">
<s:file name="file1" label="file1"></s:file><input type="button" value="add more" onClick="addMore()">
</td>
</tr>
<tr>
<td>
<s:submit name="submit" label="提交"></s:submit>
</td>
<td>
</td>
</tr>
</table>
</s:form>
</body>
</html>
这里是动态上传文件 ,可多可少[code=Java]
public class UploadAction extends ActionSupport {
private String username;
private String password;
private List<File> file1;
private List<String> file1FileName;
private List<String> file1ContentType; public String getUsername() {
return username;
} public void setUsername(String username) {
this.username = username;
} public String getPassword() {
return password;
} public void setPassword(String password) {
this.password = password;
} public List<File> getFile1() {
return file1;
} public void setFile1(List<File> file1) {
this.file1 = file1;
} public List<String> getFile1FileName() {
return file1FileName;
} public void setFile1FileName(List<String> file1FileName) {
this.file1FileName = file1FileName;
} public List<String> getFile1ContentType() {
return file1ContentType;
} public void setFile1ContentType(List<String> file1ContentType) {
this.file1ContentType = file1ContentType;
} @Override
public String execute() throws Exception {
// TODO Auto-generated method stub
for(int i = 0 ;i < file1.size() ; ++i)
{
InputStream is = new FileInputStream(file1.get(i));
ServletActionContext.getRequest().setCharacterEncoding("gb2312");
String root = ServletActionContext.getRequest().getRealPath("/upload");
System.out.println(root);
File deskfile = new File(root,this.getFile1FileName().get(i));
OutputStream os = new FileOutputStream(deskfile);
byte[] buffer = new byte[400];
int length = 0;
while((length = is.read(buffer)) > 0)
{
os.write(buffer, 0, length);
}
os.close();
is.close();
}
return SUCCESS;
}
}
[/code]
用js实现页面效果
代码示例:<script src="<c:url value='/jsp/scripts/ajaxupload.js' />" type="text/javascript"></script><script>
$(document).ready(
function(){
$("#assetDt").datepicker({dateFormat: 'yy-mm-dd'});
$("#assetExpired").datepicker({dateFormat: 'yy-mm-dd'});
new AjaxUpload('#uploadBtn', {
action: 'assetUsing.action?method=uploadImg',
name: 'imgFile',
onComplete : function(imgName,response){
$('<li></li>').html(imgName).appendTo($('#uploadInfoDiv .files'));
//alert("here2=="+response);
$("#assetOsImgPath").attr("value",response);
}
});
});
<input name="assetOsImgPath" id="assetOsImgPath" type="hidden" value=''></input> <div id="uploadBtn" name="uploadBtn" >上传</div> <p>已上传:
<ol class="files"></ol>
FileOutputStream fos = new FileOutputStream(getSavePath()+"\\"+getUploadFileName());
FileInputStream fis = new FileInputStream(getUpload());
byte[] buffer = new byte[1024];
int len = 0 ;
while((len=fis.read(buffer))>0)
{
fos.write(buffer,0,len);
}