当前位置:首页 » 文件管理 » java文件上传远程服务器

java文件上传远程服务器

发布时间: 2023-03-15 11:43:30

‘壹’ java怎么实现上传文件到服务器

  • common-fileupload是jakarta项目组开发的一个功能很强大的上传文件组件

  • 下面先介绍上传文件到服务器(多文件上传):

  • import javax.servlet.*;

  • import javax.servlet.http.*;

  • import java.io.*;

  • import java.util.*;

  • import java.util.regex.*;

  • import org.apache.commons.fileupload.*;


  • public class upload extends HttpServlet {

  • private static final String CONTENT_TYPE = "text/html; charset=GB2312";

  • //Process the HTTP Post request

  • public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

  • response.setContentType(CONTENT_TYPE);

  • PrintWriter out=response.getWriter();

  • try {

  • DiskFileUpload fu = new DiskFileUpload();

  • // 设置允许用户上传文件大小,单位:字节,这里设为2m

  • fu.setSizeMax(2*1024*1024);

  • // 设置最多只允许在内存中存储的数据,单位:字节

  • fu.setSizeThreshold(4096);

  • // 设置一旦文件大小超过getSizeThreshold()的值时数据存放在硬盘的目录

  • fu.setRepositoryPath("c://windows//temp");

  • //开始读取上传信息

  • List fileItems = fu.parseRequest(request);

  • // 依次处理每个上传的文件

  • Iterator iter = fileItems.iterator();

  • //正则匹配,过滤路径取文件名

  • String regExp=".+////(.+)$";

  • //过滤掉的文件类型

  • String[] errorType={".exe",".com",".cgi",".asp"};

  • Pattern p = Pattern.compile(regExp);

  • while (iter.hasNext()) {

  • FileItem item = (FileItem)iter.next();

  • //忽略其他不是文件域的所有表单信息

  • if (!item.isFormField()) {

  • String name = item.getName();

  • long size = item.getSize();

  • if((name==null||name.equals("")) && size==0)

  • continue;

  • Matcher m = p.matcher(name);

  • boolean result = m.find();

  • if (result){

  • for (int temp=0;temp<ERRORTYPE.LENGTH;TEMP++){

  • if (m.group(1).endsWith(errorType[temp])){

  • throw new IOException(name+": wrong type");

  • }

  • }

  • try{

  • //保存上传的文件到指定的目录

  • //在下文中上传文件至数据库时,将对这里改写

  • item.write(new File("d://" + m.group(1)));

  • out.print(name+" "+size+"");

  • }

  • catch(Exception e){

  • out.println(e);

  • }

  • }

  • else

  • {

  • throw new IOException("fail to upload");

  • }

  • }

  • }

  • }

  • catch (IOException e){

  • out.println(e);

  • }

  • catch (FileUploadException e){

  • out.println(e);

  • }

  • }

  • }

  • 现在介绍上传文件到服务器,下面只写出相关代码:

  • sql2000为例,表结构如下:

  • 字段名:name filecode

  • 类型: varchar image

  • 数据库插入代码为:PreparedStatement pstmt=conn.prepareStatement("insert into test values(?,?)");

  • 代码如下:

  • 。。。。。。

  • try{

  • 这段代码如果不去掉,将一同写入到服务器中

  • //item.write(new File("d://" + m.group(1)));

  • int byteread=0;

  • //读取输入流,也就是上传的文件内容

  • InputStream inStream=item.getInputStream();

  • pstmt.setString(1,m.group(1));

  • pstmt.setBinaryStream(2,inStream,(int)size);

  • pstmt.executeUpdate();

  • inStream.close();

  • out.println(name+" "+size+" ");

  • }

  • 。。。。。。

  • 这样就实现了上传文件至数据库

‘贰’ java 实现文件上传到另一台服务器,该怎么解决

上传本地文件代码誉渗源
使用步骤如下:
1.调用AddFile函数添加喊镇本地文件,注意路径需要使庆态用双斜框(\\)
2.调用PostFirst函数开始上传文件。
JavaScript code?<script type="text/javascript" language="javascript"> var fileMgr = new HttpUploaderMgr(); fileMgr.Load();//加载控件 window.onload = function() { fileMgr.Init();//初始化控件 //添加一个本地文件 fileMgr.AddFile("D:\\Soft\\QQ2010.exe"); fileMgr.PostFirst(); };</script>

‘叁’ java怎么将上传文件到别的服务器

首先,获得别的服务器的上传接口,然后做写上传程序的时候默认设置上传到该服务器。或者直接将java上传程序放在别的服务器,直接这里调用即可。

‘肆’ java怎么实现上传附件的功能

上传附件,实际上就是将文件存储到远程服务器,进行临时存储。举例:
**
* 上传文件
*
* @param fileName
* @param plainFilePath 文件路径路径
* @param filepath
* @return
* @throws Exception
*/
public static String fileUploadByftp(String plainFilePath, String fileName, String filepath) throws Exception {
FileInputStream fis = null;
ByteArrayOutputStream bos = null;
FTPClient ftpClient = new FTPClient();
String bl = "false";
try {
fis = new FileInputStream(plainFilePath);
bos = new ByteArrayOutputStream(fis.available());
byte[] buffer = new byte[1024];
int count = 0;
while ((count = fis.read(buffer)) != -1) {
bos.write(buffer, 0, count);
}
bos.flush();
Log.info("加密上传文件开始");
Log.info("连接远程上传服务器"+CCFCCBUtil.CCFCCBHOSTNAME+":"+22);
ftpClient.connect(CCFCCBUtil.CCFCCBHOSTNAME, 22);
ftpClient.login(CCFCCBUtil.CCFCCBLOGINNAME, CCFCCBUtil.CCFCCBLOGINPASSWORD);
FTPFile[] fs;
fs = ftpClient.listFiles();
for (FTPFile ff : fs) {
if (ff.getName().equals(filepath)) {
bl="true";
ftpClient.changeWorkingDirectory("/"+filepath+"");
}
}
Log.info("检查文件路径是否存在:/"+filepath);
if("false".equals(bl)){
ViewUtil.dataSEErrorPerformedCommon( "查询文件路径不存在:"+"/"+filepath);
return bl;
}
ftpClient.setBufferSize(1024);
ftpClient.setControlEncoding("GBK");
// 设置文件类型(二进制)
ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
ftpClient.storeFile(fileName, fis);
Log.info("上传文件成功:"+fileName+"。文件保存路径:"+"/"+filepath+"/");
return bl;
} catch (Exception e) {
throw e;
} finally {
if (fis != null) {
try {
fis.close();
} catch (Exception e) {
Log.info(e.getLocalizedMessage(), e);
}
}
if (bos != null) {
try {
bos.close();
} catch (Exception e) {
Log.info(e.getLocalizedMessage(), e);
}
}
}
}
备注:只需要修改上传的服务器地址、用户名、密码即可进行服务器访问上传。根据实际需要修改即可。

‘伍’ java怎么把文件传输到服务器

String realpath = ServletActionContext.getServletContext().getRealPath("/upload") ;//获取服务器路径
String[] targetFileName = uploadFileName;
for (int i = 0; i < upload.length; i++) {
File target = new File(realpath, targetFileName[i]);
FileUtils.File(upload[i], target);
//这是一个文件复制类File()里面就是IO操作,如果你不用这个类也可以自己写一个IO复制文件的类
}

其中private File[] upload;// 实际上传文件

private String[] uploadContentType; // 文件的内容类型

private String[] uploadFileName; // 上传文件名

这三个参数必须这样命名,因为文件上传控件默认是封装了这3个参数的,且在action里面他们应有get,set方法

‘陆’ java上传图片到远程服务器上,怎么解决呢

需要这样的一个包 jcifs-1.1.11
public static void forcdt(String dir){
InputStream in = null;
OutputStream out = null;
File localFile = new File(dir);
try{
//创建file类 传入本地文件路径
//获得本地文件的名字
String fileName = localFile.getName();
//将本地文件的名字和远程目录的名字拼接在一起
//确保上传后的文件于本地文件名字相同
SmbFile remoteFile = new SmbFile("smb://administrator:[email protected]/e$/aa/");
//创建读取缓冲流把本地的文件与程序连接在一起
in = new BufferedInputStream(new FileInputStream(localFile));
//创建一个写出缓冲流(注意jcifs-1.3.15.jar包 类名为Smb开头的类为控制远程共享计算机"io"包)
//将远程的文件路径传入SmbFileOutputStream中 并用 缓冲流套接
out = new BufferedOutputStream(new SmbFileOutputStream(remoteFile+"/"+fileName));
//创建中转字节数组
byte[] buffer = new byte[1024];
while(in.read(buffer)!=-1){//in对象的read方法返回-1为 文件以读取完毕
out.write(buffer);
buffer = new byte[1024];
}
}catch(Exception e){
e.printStackTrace();
}finally{
try{
//注意用完操作io对象的方法后关闭这些资源,走则 造成文件上传失败等问题。!
out.close();
in.close();
}catch(Exception e){
e.printStackTrace();}
}
}

‘柒’ Java怎样实现跨服务器文件上传

另一台机器也要有处理文件上传的WEB程序,你可以参考Stream上传插件(支持HTML5和Flash两种方式上传)

Stream 上传插件
Stream 是解决不同浏览器上传文件的插件,是Uploadify的Flash版和Html5版的结合!

Stream 简介
Stream 是根据某网的文件上传插件加工而来,支持不同平台(Windows, Linux, Mac, Android, iOS)下,主流浏览器(IE7+, Chrome, Firefox, Safari, 其他)的上传工作,当然在Html5标准下,还支持文件的断点续传功能,有效解决大文件的Web上传问题!

主要特征
1. 源码完全开放,目前有Java、PHP、Perl三种后台语言实现

2. 支持HTML5、Flash两种方式(跨域)上传

3. 多文件一起上传

4. HTML5支持断点续传,拖拽等新特性

5. 兼容性好IE7+, FF3.6+, Chrome*,Safari4+,遨游等主流浏览器

6. 进度条、速度、剩余时间等附属信息

7. `选择文件的按钮`可以自定义

8. 简单的参数配置实现 灵活多变的功能

9. 支持文件夹上传(Chrome21+, Opera15+)

10. 支持自定义UI(V1.4+)

指定跨域上传就可以了

‘捌’ java中怎么把文件上传到服务器的指定路径

文件从本地到服务器的功能,其实是为了解决目前浏览器不支持获取本地文件全路径。不得已而想到上传到服务器的固定目录,从而方便项目获取文件,进而使程序支持EXCEL批量导入数据。

java中文件上传到服务器的指定路径的代码:

在前台界面中输入:

<form method="post" enctype="multipart/form-data" action="../manage/excelImport.do">

请选文件:<input type="file" name="excelFile">

<input type="submit" value="导入" onclick="return impExcel();"/>

</form>

action中获取前台传来数据并保存

/**

* excel 导入文件明颂

* @return

* @throws IOException

*/

@RequestMapping("/usermanager/excelImport.do")

public String excelImport(

String filePath,

MultipartFile excelFile,HttpServletRequest request) throws IOException{

log.info("<<悉孙<<<<action:{} Method:{} start>>>>>>","usermanager","excelImport" );

if (excelFile != null){

String filename=excelFile.getOriginalFilename();

String a=request.getRealPath("u/cms/www/201509");

SaveFileFromInputStream(excelFile.getInputStream(),request.getRealPath("u/cms/www/201509"),filename);//保存到服激陆郑务器的路径

}

log.info("<<<<<<action:{} Method:{} end>>>>>>","usermanager","excelImport" );

return "";

}

/**

* 将MultipartFile转化为file并保存到服务器上的某地

*/

public void SaveFileFromInputStream(InputStream stream,String path,String savefile) throws IOException

{

FileOutputStream fs=new FileOutputStream( path + "/"+ savefile);

System.out.println("------------"+path + "/"+ savefile);

byte[] buffer =new byte[1024*1024];

int bytesum = 0;

int byteread = 0;

while ((byteread=stream.read(buffer))!=-1)

{

bytesum+=byteread;

fs.write(buffer,0,byteread);

fs.flush();

}

fs.close();

stream.close();

}

‘玖’ Java怎么上传文件到远程Windows服务器

安装一个FTP就可以直接上传下载了,你可以去服务器厂商(正睿)的网上找找相关技术文档参考一下,很快就清楚了!

‘拾’ javaweb 怎么样将本地文件传输到远程服务器

可以通过JDK自带的API实现,如下代码:
package com.cloudpower.util;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

import sun.net.TelnetInputStream;
import sun.net.TelnetOutputStream;
import sun.net.ftp.FtpClient;

/**
* Java自带的API对FTP的操作
* @Title:Ftp.java
*/
public class Ftp {
/**
* 本地文件名
*/
private String localfilename;
/**
* 远程文件名
*/
private String remotefilename;
/**
* FTP客户端
*/
private FtpClient ftpClient;

/**
* 服务器连接
* @param ip 服务器IP
* @param port 服务器端口
* @param user 用户名
* @param password 密码
* @param path 服务器路径
* @date 2012-7-11
*/
public void connectServer(String ip, int port, String user,
String password, String path) {
try {
/* ******连接服务器的两种方法*******/
//第一种方法
// ftpClient = new FtpClient();
// ftpClient.openServer(ip, port);
//第二种方法
ftpClient = new FtpClient(ip);

ftpClient.login(user, password);
// 设置成2进制传输
ftpClient.binary();
System.out.println("login success!");
if (path.length() != 0){
//把远程系统上的目录切换到参数path所指定的目录
ftpClient.cd(path);
}
ftpClient.binary();
} catch (IOException ex) {
ex.printStackTrace();
throw new RuntimeException(ex);
}
}
public void closeConnect() {
try {
ftpClient.closeServer();
System.out.println("disconnect success");
} catch (IOException ex) {
System.out.println("not disconnect");
ex.printStackTrace();
throw new RuntimeException(ex);
}
}
public void upload(String localFile, String remoteFile) {
this.localfilename = localFile;
this.remotefilename = remoteFile;
TelnetOutputStream os = null;
FileInputStream is = null;
try {
//将远程文件加入输出流中
os = ftpClient.put(this.remotefilename);
//获取本地文件的输入流
File file_in = new File(this.localfilename);
is = new FileInputStream(file_in);
//创建一个缓冲区
byte[] bytes = new byte[1024];
int c;
while ((c = is.read(bytes)) != -1) {
os.write(bytes, 0, c);
}
System.out.println("upload success");
} catch (IOException ex) {
System.out.println("not upload");
ex.printStackTrace();
throw new RuntimeException(ex);
} finally{
try {
if(is != null){
is.close();
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if(os != null){
os.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
public void download(String remoteFile, String localFile) {
TelnetInputStream is = null;
FileOutputStream os = null;
try {
//获取远程机器上的文件filename,借助TelnetInputStream把该文件传送到本地。
is = ftpClient.get(remoteFile);
File file_in = new File(localFile);
os = new FileOutputStream(file_in);
byte[] bytes = new byte[1024];
int c;
while ((c = is.read(bytes)) != -1) {
os.write(bytes, 0, c);
}
System.out.println("download success");
} catch (IOException ex) {
System.out.println("not download");
ex.printStackTrace();
throw new RuntimeException(ex);
} finally{
try {
if(is != null){
is.close();
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if(os != null){
os.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}

public static void main(String agrs[]) {

String filepath[] = { "/temp/aa.txt", "/temp/regist.log"};
String localfilepath[] = { "C:\\tmp\\1.txt","C:\\tmp\\2.log"};

Ftp fu = new Ftp();
/*
* 使用默认的端口号、用户名、密码以及根目录连接FTP服务器
*/
fu.connectServer("127.0.0.1", 22, "anonymous", "IEUser@", "/temp");

//下载
for (int i = 0; i < filepath.length; i++) {
fu.download(filepath[i], localfilepath[i]);
}

String localfile = "E:\\号码.txt";
String remotefile = "/temp/哈哈.txt";
//上传
fu.upload(localfile, remotefile);
fu.closeConnect();
}
}

热点内容
编译原理课时设置 发布:2025-05-18 04:13:28 浏览:370
linux中进入ip地址服务器 发布:2025-05-18 04:11:21 浏览:606
java用什么软件写 发布:2025-05-18 03:56:19 浏览:27
linux配置vim编译c 发布:2025-05-18 03:55:07 浏览:100
砸百鬼脚本 发布:2025-05-18 03:53:34 浏览:935
安卓手机如何拍视频和苹果一样 发布:2025-05-18 03:40:47 浏览:727
为什么安卓手机连不上苹果7热点 发布:2025-05-18 03:40:13 浏览:797
网卡访问 发布:2025-05-18 03:35:04 浏览:504
接收和发送服务器地址 发布:2025-05-18 03:33:48 浏览:366
ef数据库查询数据 发布:2025-05-18 03:29:36 浏览:668