上传文件到指定的服务器
url为服务器的ip
username为服务器的登录名
password为服务器的密码
path为要上传的服务器绝对路径 /home/xxxx/file
filename 文件名的数组
filepath 文件路径的数组,
现在ie8,firefox的安全机制都使得现在的上传文件或许不到绝对路径,当然如果直接上传到tomcat服务器的时候例外.
如果上传的地方与tomcat没有任何关系,就是要上传到linux的一个指定文件夹下的话,这样就比较麻烦了,因为上传需要的是绝对路径,而现在的浏览器又获取不到.
这时候就需要先把文件利用struts上传文件到tomcat下的一个临时目录,再通过ServletActionContext.getRequest().getRealPath("/tmp")获取临时文件夹的路径加上文件名.这就是filepath的由来。
为防止文件名冲突所带的影响,就采用的时间long型作为文件名,当然数据库存的时候要指定一下
public static String[] uploadFile(String url, String username,
String password, String path, String[] filename, String[] filepath) {
Timestamp d = new Timestamp(System.currentTimeMillis());
String[] uppath = new String[filename.length];
TelnetOutputStream os = null;
FtpClient ftpClient = new FtpClient();
try {
ftpClient.openServer(url);
ftpClient.login(username, password);
ftpClient.cd(path);
ftpClient.binary();
for (int i = 0; i < filename.length; i++) {
FileInputStream in = new FileInputStream(new File(
filepath[i]));
uppath[i] = d.getTime() + "." + getFileHouZhui(filename[i]);
try {
// 命名文件
os = ftpClient.put(uppath[i]);
byte[] bytes = new byte[1024];
int c;
while ((c = in.read(bytes)) != -1) {
os.write(bytes, 0, c);
}
} catch (IOException e) {
} finally {
if (in != null) {
in.close();
}
if (os != null) {
os.close();
}
}
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
closeFTPClient();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return uppath;
}
public static String getFileHouZhui(String filename) {
String fn[] = filename.split("[.]");
String houzhuiname = fn[fn.length - 1];
return houzhuiname;
}
ACTION下载文件
这个是利用浏览器的下载功能,需先从配置文件中获取ftp所需的基本信息 ip 登录名 密码 路径
public String downFile() throws Exception {
String username = CommonProperties.getStringProperty("username");
String password = CommonProperties.getStringProperty("password");
String ip = CommonProperties.getStringProperty("ip");
String path = CommonProperties.getStringProperty("path");
response.setContentType("text/html");
response.setCharacterEncoding("utf-8");
response.setContentType("apploaction/x-download");
String filepath = new String(getDownpath().getBytes("ISO-8859-1"),"UTF-8");
String filename = filepath;
response.setHeader("Content-disposition", "attachment;filename="
+ new String(filename.getBytes("utf-8"), "ISO-8859-1"));
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
try {
FtpClient ftp = new FtpClient(ip);
ftp.login(username, password);
ftp.binary();
if (path.length() != 0) {
ftp.cd(path);
}
ServletOutputStream sout = response.getOutputStream();
TelnetInputStream is = ftp.get(filepath);
bis = new BufferedInputStream(is);
bos = new BufferedOutputStream(sout);
byte[] bytes = new byte[1024];
int c;
while ((c = bis.read(bytes)) != -1) {
bos.write(bytes, 0, c);
}
bos.close();
is.close();
bis.close();
} catch (IOException ex) {
ex.printStackTrace();
}
return null;
}
本文介绍如何使用Struts框架实现文件上传至Tomcat服务器的临时目录,并进一步上传到远程Linux服务器的方法,同时提供了通过浏览器下载FTP文件的实现方式。
756

被折叠的 条评论
为什么被折叠?



