dwr框架js函数参数为流时该怎么设置?
1个回答
展开全部
1、写好后台代码如下:接受fileTransfer对象,如果上传单个使用单个对象即可
public String fileUploadForDwr(ArrayList<FileTransfer> fileTransfer) {
String tmp = "error";
String fileName = ""; // 文件名
String newFileName = ""; // 新文件名
InputStream streamIn = null;
OutputStream streamOut = null;
long size = 1024 * 1024 * 5;
if(fileTransfer.size() > size){
return "文件不得大于5M";
}
WebContext webContext = WebContextFactory.get();
String saveurl = webContext.getHttpServletRequest().getSession()
.getServletContext().getRealPath("/upload");
for (FileTransfer obj : fileTransfer) {
if (obj.getFilename() != null && !obj.getFilename().equals("")) {
fileName = obj.getFilename();
if (fileName.lastIndexOf("\\") > -1) {
fileName = fileName
.substring(fileName.lastIndexOf("\\") + 1);
}
System.out.println(fileName);
newFileName = CTools.createID()
+ fileName.substring(fileName.indexOf("."));
try {
streamIn = obj.getInputStream();
streamOut = new FileOutputStream(new File(saveurl,
newFileName));
int bytesRead = 0;
byte[] buffer = new byte[81920];
while ((bytesRead = streamIn.read(buffer, 0, 81920)) != -1) {
streamOut.write(buffer, 0, bytesRead);
}
streamOut.close();
streamIn.close();
tmp="success";
} catch (FileNotFoundException ex) {
} catch (IOException e) {
// TODO: handle exception
} finally {
streamIn = null;
streamOut = null;
}
}
}
return tmp;
}
2、页面上添加脚本var file=dwr.util.getValue("attach"); 其中attach为file空间的名称
function uploadFile(){
var file=dwr.util.getValue("attach");
var str="123"
var fileArr = new Array();//注意这里是用的集合,无论一个附件还是多个附件都可以了
fileArr[0] = file;
dwrFileUtil.fileUploadForDwr(fileArr,{callback:function (data){back(str,data)}});
}
function back(tmp,data){
alert(3);
alert(tmp+""+data);
}
public String fileUploadForDwr(ArrayList<FileTransfer> fileTransfer) {
String tmp = "error";
String fileName = ""; // 文件名
String newFileName = ""; // 新文件名
InputStream streamIn = null;
OutputStream streamOut = null;
long size = 1024 * 1024 * 5;
if(fileTransfer.size() > size){
return "文件不得大于5M";
}
WebContext webContext = WebContextFactory.get();
String saveurl = webContext.getHttpServletRequest().getSession()
.getServletContext().getRealPath("/upload");
for (FileTransfer obj : fileTransfer) {
if (obj.getFilename() != null && !obj.getFilename().equals("")) {
fileName = obj.getFilename();
if (fileName.lastIndexOf("\\") > -1) {
fileName = fileName
.substring(fileName.lastIndexOf("\\") + 1);
}
System.out.println(fileName);
newFileName = CTools.createID()
+ fileName.substring(fileName.indexOf("."));
try {
streamIn = obj.getInputStream();
streamOut = new FileOutputStream(new File(saveurl,
newFileName));
int bytesRead = 0;
byte[] buffer = new byte[81920];
while ((bytesRead = streamIn.read(buffer, 0, 81920)) != -1) {
streamOut.write(buffer, 0, bytesRead);
}
streamOut.close();
streamIn.close();
tmp="success";
} catch (FileNotFoundException ex) {
} catch (IOException e) {
// TODO: handle exception
} finally {
streamIn = null;
streamOut = null;
}
}
}
return tmp;
}
2、页面上添加脚本var file=dwr.util.getValue("attach"); 其中attach为file空间的名称
function uploadFile(){
var file=dwr.util.getValue("attach");
var str="123"
var fileArr = new Array();//注意这里是用的集合,无论一个附件还是多个附件都可以了
fileArr[0] = file;
dwrFileUtil.fileUploadForDwr(fileArr,{callback:function (data){back(str,data)}});
}
function back(tmp,data){
alert(3);
alert(tmp+""+data);
}
追问
这个dwr.xml中是怎么设置的?如果是FileTransfer来接受,dwr.jar包一定得是3.0版本以上
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询