2个ftp服务器传输文件跨域吗,跨域上传图片之FTP上传

本文介绍了一种通过FTP实现跨域图片上传的方法,并详细展示了如何利用Java进行FTP连接、文件上传及目录创建等操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

跨域上传图片之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="" />

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值