下面是js 和后台java完整代码:
function uploadImage()
{
uuid = new UUID().createUUID();
var fileName = $("#videoImage").val();
var array1=fileName.split(".");
if(array1[array1.length-1]=="jpg"||array1[array1.length-1]=="JPG")
{
$.ajaxFileUpload({
url:‘upload!uploadImage.action‘,
secureuri:false,
files:[$(‘#videoImage‘)],
dataType: ‘json‘,
global: false,
data:{"videoImageFileName1":uuid+"."+array1[1],"uuid":uuid},
complete:function()
{
// alert("上传成功");
$("#image").attr("src","tem/"+username+"/"+uuid+"."+array1[1]+".jpg?"+Math.random());
$("#xttp").attr("src","tem/"+username+"/"+uuid+"."+array1[1]+".jpg?"+Math.random());
$("#picName").val(fileName);
$("#lla").html(fileName);
//$("#videoImage").replaceWith(‘<input type="file" name="videoImage" style="width: 455px" id="videoImage" onchange="uploadImage()" class="uplod" />‘);
}
});
}else
{
alert(‘请选择jpg图片‘);
}
}
//ftp 上传
private boolean ftpUpLoad(String ip, String userName, String password,
File srcFile, String directory) {
boolean res = false;
FTPClient ftpClient = new FTPClient();//ftp客户端
try {
int indexOf = directory.indexOf("/");
String user = directory.substring(0, indexOf);//获取上传路径
String md5Str = directory.substring(indexOf + 1);
ftpClient.connect(ip);//根据ip创建连接
boolean r0 = ftpClient.login(userName, password);//ftp登录
boolean r1 = ftpClient.makeDirectory("/img/" + user);//ftp创建文件夹
boolean r2 = ftpClient.makeDirectory("/img/" + user + "/"+ md5Str);//ftp创建加密文件夹
boolean r3 = ftpClient.changeWorkingDirectory("/img/" + user+ "/" + md5Str);//ftp改变工作目录
System.out.println("FTP img:"+directory+" "+r0+" "+r1+" "+r2+" "+r3);
ftpClient.setBufferSize(1024);//设置流大小
ftpClient.setSoTimeout(3600000);//设置延时
ftpClient.setDefaultTimeout(3600000);//设置默认超时时间
ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);//设置文件类型
String rName = "img.jpg";
FileInputStream fileInputStream = new FileInputStream(srcFile);//获取文输入流
res = ftpClient.storeFile(new String(rName.getBytes("UTF-8"),"iso-8859-1"), fileInputStream);//上传文件
fileInputStream.close();//文件输入流关闭
ftpClient.logout();//退登陆
} catch (Exception e) {
e.printStackTrace();
} finally {
if (ftpClient.isConnected()) {//关闭客户端
try {
ftpClient.disconnect();//关闭连接
} catch (IOException e) {
e.printStackTrace();
}
}
}
return res;
}
}
版权声明:本文为博主原创文章,未经博主允许不得转载。