跨域上传图片之FTP上传
1、
实现的功能就是跨域上传图片,然后可以将处理的结果正确的放回到请求端(如图片名称、路径等)
2、
首先准备将commons-net-ftp-2.0.jar包准备一下
3、前端jsp
language="java" import="java.util.*" pageEncoding="UTF-8"%>
String path = request.getContextPath();
String basePath =
request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
/p>
HTML 4.01 Transitional//EN">
3、
struts配置文件
method="ftpUpload">
/index_ftp.jsp
/index_ftp.jsp
4、
action代码如下
package com.action;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.SocketException;
import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPReply;
import sw.common.util.ConfigDataInfo;
public class FTPAction extends BaseAction {
private static final long serialVersionUID = 1L;
private FTPClient ftpClient;
private String bufferSize;
private String contentType;
private File myFile;
private String myFileFileName;
private String myFileContentType;
public String ftpUpload() {
String server =
ConfigDataInfo.getConfigValue("server");
int port =
Integer.parseInt(ConfigDataInfo.getConfigValue("port"));
String user =
ConfigDataInfo.getConfigValue("user");
String password =
ConfigDataInfo.getConfigValue("password");
String path =
ConfigDataInfo.getConfigValue("path");
File file_in = myFile;
this.ftpUploadFile(server, port, user, password, path,
file_in);
return SUCCESS;
}
public String ftpUploadFile(String
server,int port,String user,String password,String path,File
file_in) {
if (!file_in.isFile()) {
// 是否文件,或找不到指定文件
this.addActionError("customer.electroFile.errors.FileNotFound");
return "";
}else if(!fileSize(file_in)){
//控制文件大小
return "";
}
//文件名
String file_inName="";
//登录FTP服务器
this.connectServer(server, port, user,
password);
//有出错信息
if (hasActionErrors()) {
return "";
}
try {
if (path != null) {
// 检查path最后有没有分隔符
path = this.pathAddress(path);
//新建路径
// FTP转到PATH路径
this.mkdir(server, port, user, password,
path);
ftpClient.changeWorkingDirectory(gbktoiso8859(path));
} else {
this.addActionError(getText("customer.electroFile.errors.ftpPath"));
closeConnect();
return "";
}
// 给文件名加上时间撮
file_inName = this.getMyFileFileName();
final int lastIndex =
file_inName.lastIndexOf(".");
file_inName = file_inName.substring(0, lastIndex) +
"_"
+ System.currentTimeMillis()
+ file_inName.substring(lastIndex);
FileInputStream fileIS=new FileInputStream(file_in);
boolean flag=ftpClient.storeFile(gbktoiso8859(file_inName),
fileIS);
if (!flag) {
closeConnect();
this.addActionError("customer.electroFile.errors.FileUpload");
}
}catch(final FileNotFoundException fnfe){
this.addActionError("customer.electroFile.errors.FileNotFound");
} catch (final IOException ex) {
this.addActionError(getText("customer.electroFile.errors.Connect"));
}finally{
//退出FTP服务器
this.closeConnect();
}
returnfile_inName;
}
public boolean mkdir(String server,int port,String user,String password,String
path){
this.connectServer(server, port, user,
password);
if (hasActionErrors()) {
return false;
}
try {
if (path!=null) {
//检查path最后有没有分隔符
path=this.pathAddress(path);
}else {
this.addActionError(getText("customer.electroFile.errors.ftpPath"));
return false;
}
boolean flag=ftpClient.makeDirectory(gbktoiso8859(path));
//错误也不给予提示
return true;
} catch (final IOException ex) {
this.addActionError(getText("customer.electroFile.errors.mkdir"));
}
return false;
}
private void connectServer(String server, int port, String user, String password) {
if (ftpClient == null) {
try {
ftpClient=new FTPClient();
//所有使用iso-8859-1做为通讯编码集
//ftpClient.setControlEncoding("iso-8859-1");
//ftpClient.configure(getFTPClientConfig());
// 连接FTP服务器
ftpClient.connect(server, port);
// 登录FTP服务器
if(!ftpClient.login(user, password)){
this.addActionError(getText("customer.electroFile.errors.logonFTP"));
}
//状态
int reply = ftpClient.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply))
{
closeConnect();
this.addActionError(getText("customer.electroFile.errors.logonFTP"));
}
// 用被动模式传输
ftpClient.enterLocalPassiveMode();
// 将文件传输类型设置为二进制
ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
//防止server超时断开
//ftpClient.setDefaultTimeout(60000);
//10超时
ftpClient.setSoTimeout(10000);
}catch(SocketException se){
this.addActionError(getText("customer.electroFile.errors.logonFTP"));
closeConnect();
}catch (final IOException ex) {
this.addActionError(getText("customer.electroFile.errors.logonFTP"));
closeConnect();
}
}
}
public String pathAddress(String path){
if (path==null || path.length()<1) {
return "";
}
String temp_path=path.substring(path.length()-1);
if (!temp_path.equals("/") &&
!temp_path.equals("\\")) {
temp_path=path+File.separator;
}else{
temp_path=path;
}
return temp_path;
}
private static String gbktoiso8859(Object obj) {
try {
if (obj == null)
return "";
else
return new String(obj.toString().getBytes("GBK"), "iso-8859-1");
} catch (Exception e) {
return "";
}
}
private void closeConnect() {
try {
if (ftpClient != null) {
ftpClient.logout();
ftpClient.disconnect();
}
} catch (Exception e) {
}
}
private boolean fileSize(File file_in) {
if (file_in == null || file_in.length() == 0) {
addActionError(getText("customer.electroFile.errors.fileUnallowed"));
return false;
} else {
if (file_in.length() > (1024 * 1024 *
5)){
addActionError(getText("customer.electroFile.errors.fileTooLarge"));
return false;
}
}
return true;
}
5、参数配置文件
value="http://192.2.9.74:8090/gameshop"
/>
value="http://192.2.9.74:8090/gameshop"
/>
value="192.2.11.238" />
value="develop" />
value="poleved" />
value="21" />
value="" />