思路就是用一个隐藏的iframe来上传文件,然后用一个servlet来接收上传的文件,再用一个实现了ProgressListener接口的方法来更新存在session里的上传进度信息,然后通过另一个servlet或者action来读取当前上传进度,前台通过ajax请求这个servlet或action来读取当前的进度来进行展示上面是有人给我的思路 但是对于一个菜鸟来说还是有很大的问题的!请问怎么写action的啊
public String photosAdd(){
for(int i = 0 ;i<photos.length;i++){
String fileName = photosFileName[i];
NewsPhotos newsPhotos = new NewsPhotos();
newsPhotos.setFileName(fileName);
String extName = FileTypeUtil.getFileExtName(fileName);
String newsName = UUID.randomUUID()+"."+ extName;
String virtualpath = "/upfiles"+newsPhotos.getFileDir()+"/"+FileTypeUtil.getFileFolder(newsName);
String realPath = ServletActionContext.getServletContext().getRealPath(virtualpath);
File saveDir = new File(realPath);
if(!saveDir.exists()) saveDir.mkdirs();
try {
FileUtil.copyFile(photos[i], new File(realPath+"\\"+newsPhotos.getFileName()));
} catch (IOException e) {
e.printStackTrace();
}
Long fileSize = photos[i].length();
newsPhotos.setFileSize(fileSize);
newsPhotos.setOriginalName(fileName);
newsPhotos.setNews(news);
newsPhotosService.add(newsPhotos);
}
this.message="图片添加成功";
return "message";
}这是我写的 但是有错的 有没有类似的 给看看啊
public String photosAdd(){
for(int i = 0 ;i<photos.length;i++){
String fileName = photosFileName[i];
NewsPhotos newsPhotos = new NewsPhotos();
newsPhotos.setFileName(fileName);
String extName = FileTypeUtil.getFileExtName(fileName);
String newsName = UUID.randomUUID()+"."+ extName;
String virtualpath = "/upfiles"+newsPhotos.getFileDir()+"/"+FileTypeUtil.getFileFolder(newsName);
String realPath = ServletActionContext.getServletContext().getRealPath(virtualpath);
File saveDir = new File(realPath);
if(!saveDir.exists()) saveDir.mkdirs();
try {
FileUtil.copyFile(photos[i], new File(realPath+"\\"+newsPhotos.getFileName()));
} catch (IOException e) {
e.printStackTrace();
}
Long fileSize = photos[i].length();
newsPhotos.setFileSize(fileSize);
newsPhotos.setOriginalName(fileName);
newsPhotos.setNews(news);
newsPhotosService.add(newsPhotos);
}
this.message="图片添加成功";
return "message";
}这是我写的 但是有错的 有没有类似的 给看看啊
$(document).ready(function() {
$('#uploadbtn').click(function(){
//定义图片格式的数组
var imgModel = [".jpg",".jpeg",".gif",".bmp",".png"];
// 定义特殊字符
var specialChar = ["&"];
var path = getPath($('#fileload1'))
for(var i in specialChar){
var fileName = path.substring(path.lastIndexOf("/")+1,path.length);
if(fileName.indexOf(specialChar[i])>0){
alert('文件名存在特殊字符');
return false;
}
}
//获取文件的后缀并转换为小写
var suffix = path.substring(path.lastIndexOf("."),path.length).toLowerCase();
var flag=false;
alert(path);
//判断是否是图片格式
for(var i in imgModel){
if(suffix==imgModel[i]){
flag=true;
}
}
//如果是图片则上传
if (flag) {
$.ajax({
type: "POST",
url: getRootPath() + "/uploadServlet",
data: "filePath=" + path,
success: function(msg){
alert(msg);
}
});
}else{
alert('请选择图片');
}
});
/**
* 获取上传文件的真实路径
* @param {Object} obj
*/
function getPath(obj) //参数obj为input file对象
{
if(obj)
{
if (window.navigator.userAgent.indexOf("MSIE")>=1)
{
obj.select();
return document.selection.createRange().text;
}
else if(window.navigator.userAgent.indexOf("Firefox")>=1)
{
if(obj.files)
{
return obj.files.item(0).getAsDataURL();
}
return obj.value;
}
return obj.value;
}
}
function getRootPath(){
//获取当前网址,如: http://localhost:8083/uimcardprj/share/meun.jsp
var curWwwPath = window.document.location.href;
//获取主机地址之后的目录,如: uimcardprj/share/meun.jsp
var pathName = window.document.location.pathname;
var pos = curWwwPath.indexOf(pathName);
//获取主机地址,如: http://localhost:8083
var localhostPaht = curWwwPath.substring(0, pos);
//获取带"/"的项目名,如:/uimcardprj
var projectName = pathName.substring(0, pathName.substr(1).indexOf('/') + 1);
return (localhostPaht + projectName);
}
});
</script>
</head> <body>
<br>
<input type="file" name="fileload1" id="fileload1" />
<input type="button" name="uploadbtn" id="uploadbtn" value="上传文件"
style="position: absolute; left: 590px;"/>
</div>
</body>servlet request.setCharacterEncoding("UTF-8");
String filePath = (request.getParameter("filePath")).replaceAll("\\\\", "/");
String fileName = filePath.substring(filePath.lastIndexOf("/")+1,filePath.length());
FileInputStream inFile = new FileInputStream(filePath);
//存放文件的路径
FileOutputStream outFile = new FileOutputStream("d:/test/"+fileName);
//获取文件的大小
String fileSize = (inFile.available()/1024)+"KB";
int len=0;
byte[] b = new byte[8*1024];
while((len=inFile.read(b))!=-1){
outFile.write(b, 0, len);
}
outFile.flush();
outFile.close();
inFile.close();
PrintWriter out = response.getWriter();
out.print("success");
out.flush();
out.close();
看我的blog