dwr上传文件

java文件:

public String setAdmingImg(FileTransfer filetransfer){//传入的参数用来获取输入流

WebContext webcontext = WebContextFactory.get();

String save_path = webcontext.getHttpServletRequest().getSession()  

                .getServletContext().getRealPath("/img");//这个两个步骤用来设置存储路径

try {

File file = new File(save_path + "/hhh.jpg");

InputStream uploadfile = filetransfer.getInputStream();

int available = uploadfile.available();

byte[] b = new byte[available];  //字节流来上传文件

FileOutputStream foutput = new FileOutputStream(file);  

uploadfile.read(b);  

foutput.write(b);  

foutput.flush();  

foutput.close();  

uploadfile.close();  

} catch (Exception e) {

// TODO: handle exception

e.printStackTrace();

}

return save_path;

}

html:

<input type = "file" name= "file" id="file">//文件选择器

js:

function aaa(){

  var file = dwr.util.getValue("file");//用来dwr.util.getValue()来获取文件对象

  ajax.setAdmingImg(file,kkk_callback);

}

注:同名文件会发生覆盖

你可能感兴趣的:(dwr上传文件)