当前位置:首页 » 文件管理 » java上传代码吗

java上传代码吗

发布时间: 2022-12-06 07:27:41

java 文件上传的代码,尽量详细一点。。。

// 这是我写的一个方法,里面只需要传两个参数就OK了,在任何地方调用此方法都可以文件上传
/**
* 上传文件
* @param file待上传的文件
* @param storePath待存储的路径(该路径还包括文件名)
*/
public void uploadFormFile(FormFile file,String storePath)throws Exception{
// 开始上传
InputStream is =null;
OutputStream os =null;
try {
is = file.getInputStream();
os = new FileOutputStream(storePath);
int bytes = 0;
byte[] buffer = new byte[8192];
while ((bytes = is.read(buffer, 0, 8192)) != -1) {
os.write(buffer, 0, bytes);
}
os.close();
is.close();
} catch (Exception e) {
throw e;
}
finally{
if(os!=null){
try{
os.close();
os=null;
}catch(Exception e1){
;
}
}
if(is!=null){
try{
is.close();
is=null;
}catch(Exception e1){
;
}
}
}
}

② java上传文件代码

public class FileUpLoad extends ActionSupport{

//"多文件上传就用list就可以了private List<File> file;"
private File file;
//上传文本的name

public File getFile() {

return file;

}

public void setFile(File file) {

this.file = file;

}

private String fileContentType;
//上传的文件类型。

public String getFileContentType() {

return fileContentType;

}

public void setFileContentType(String fileContentType) {

this.fileContentType = fileContentType;
}

//获取上传文件的名称
private String fileFileName;

public String getFileFileName() {

return fileFileName;

}

public void setFileFileName(String fileFileName) {

this.fileFileName = fileFileName;

}

public String upload() throws Exception

{
//获取文件上传路径
String root=ServletActionContext.getRequest().getRealPath("/upload");

InputStream is=new FileInputStream(file);

String.substring(fileFileName.indexOf("."));//截取上传文件的后缀。便于新定义名称。.jpg

System.out.println(name);

File descFile=new File(root,新定义的文件名称+fileFileName.indexOf("."));

OutputStream os=new FileOutputStream(descFile);

byte[] buffer=new byte[1024];
int length=0;

while(-1!=(length=(is.read(buffer))))

{

os.write(buffer, 0, length);

}

is.close();

os.close();

return SUCCESS;

}

}

③ java图片批量上传代码

用struts也可以实现 多文件上传
下面是我写的代码,作为参考!

/*文件目录*/
public static String [] fileArray={
"logo.png",
"index.swf",
"OEMInfo.txt",
"favicon.ico"};

/**
* @author Caoshun
* @see 接收并保存文件
* */
public static void receiveAndSaveAllFileByPath(ActionForm form,String rootPath1,String rootPath2){
String fileName="";
//获取表单中的文件资源
Hashtable<Object, Object> files = form.getMultipartRequestHandler().getFileElements();
//遍历文件,并且循环保存
//当前处理文件序号
int file_num=1;
for (Enumeration<Object> e = files.keys(); e.hasMoreElements();) {

/*根据处理的当前文件下标,确定文件名*/
fileName=fileArray[file_num-1];

FormFile file = (FormFile) files.get((String) e.nextElement());
if (file != null && file.getFileSize() > 0) {
try {
//使用formfile.getInputStream()来获取一个文件的输入流进行保存。
//文件名
//String fileName = file.getFileName();
//System.out.println("debug in AddEnterpriceAction.java on line 152 fileName is : "+fileName);
//文件大小
//int fileSize = file.getFileSize();
//文件流
InputStream is = file.getInputStream();
//将输入流保存到文件
//String rootPath = this.servlet.getServletContext().getRealPath("files");

//往cn中写入
File rf = new File(rootPath1);
FileOutputStream fos = null;
fos = new FileOutputStream(new File(rf, fileName));
byte[] b = new byte[10240];
int real = 0;
real = is.read(b);
while (real > 0) {
fos.write(b, 0, real);
real = is.read(b);
}

//往en中写入
File rf2 = new File(rootPath2);
InputStream is2 = file.getInputStream();
FileOutputStream fos2 = null;
fos2 = new FileOutputStream(new File(rf2, fileName));
byte[] b2 = new byte[10240];
int real2 = 0;
real2 = is2.read(b2);
while (real2 > 0) {
fos2.write(b2, 0, real2);
real2 = is2.read(b2);
}

//关闭文件流
fos.close();
is.close();
fos2.close();
is2.close();
} catch (RuntimeException e1) {
e1.printStackTrace();
} catch (Exception ee) {
ee.printStackTrace();
}
file.destroy();

}
file_num++;
}
}

④ 如何用java代码实现ftp文件上传

import java.io.File;
import java.io.FileInputStream;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPReply;

public class test {

private FTPClient ftp;
/**
*
* @param path 上传到ftp服务器哪个路径下
* @param addr 地址
* @param port 端口号
* @param username 用户名
* @param password 密码
* @return
* @throws Exception
*/
private boolean connect(String path,String addr,int port,String username,String password) throws Exception {
boolean result = false;
ftp = new FTPClient();
int reply;
ftp.connect(addr,port);
ftp.login(username,password);
ftp.setFileType(FTPClient.BINARY_FILE_TYPE);
reply = ftp.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply)) {
ftp.disconnect();
return result;
}
ftp.changeWorkingDirectory(path);
result = true;
return result;
}
/**
*
* @param file 上传的文件或文件夹
* @throws Exception
*/
private void upload(File file) throws Exception{
if(file.isDirectory()){
ftp.makeDirectory(file.getName());
ftp.changeWorkingDirectory(file.getName());
String[] files = file.list();
for (int i = 0; i < files.length; i++) {
File file1 = new File(file.getPath()+"\\"+files[i] );
if(file1.isDirectory()){
upload(file1);
ftp.changeToParentDirectory();
}else{
File file2 = new File(file.getPath()+"\\"+files[i]);

⑤ 求JAVA上传图片代码

package com;

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.jsp.*;
import com.jspsmart.upload.*;

public class uploadfiles extends HttpServlet
{
public void doGet(HttpServletRequest request,HttpServletResponse response)
throws ServletException,IOException
{
//使用了一个第三方的组件,存放在web-inf/lib下
response.setContentType("text/html;charset=GB2312");

//由于SmartUpload的初始化方法需要pageContext,所以我们在servlet中得到他
//为了得到pageConext要首先得到JspFactory的实例
//通过JspFactory的实例的getPageContext方法得到pageConext的实例
JspFactory jf = null;

//得到JspFactory的实例
jf=JspFactory.getDefaultFactory();

/*
getPageContext(Servlet servlet,
ServletRequest request,
ServletResponse response,
java.lang.String errorPageURL,
boolean needsSession,
int buffer,
boolean autoflush)
*/
PageContext pageContext=jf.getPageContext(this,request,response,null,true,8192,true);

try
{
//实例化SmartUpload
SmartUpload mySmartUpload=new SmartUpload();

//初始化SmartUpload的实例,需要PageContext的实例
mySmartUpload.initialize(pageContext);

//设定最大上传的字节数,其实可以不进行设定,表示上传的文件没有大小限制
//mySmartUpload.setTotalMaxFileSize(10000000);
mySmartUpload.upload();

//下面是单文件上传
//上传的文件以com.jspsmart.upload.File 代表,如果文件名称重复,则进行覆盖
com.jspsmart.upload.File file=mySmartUpload.getFiles().getFile(0);
String upLoadFileName=file.getFileName();

//调用com.jspsmart.upload.File实例的saveas的方法保存文件,此时的文件名即是
//保存到服务器上的文件名
file.saveAs("/upload/"+upLoadFileName);
Request req =
Text t = .....;
t.setUpload(upLoadFileName);
t.set.....(req);
}
catch(SmartUploadException e)
{
System.out.println(e.getMessage());
}

}
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, java.io.IOException
{
doGet(request,response);
}
}

⑥ java实现文件上传,代码尽量简洁~~~~~·

普通方法实现任意上传?本地文件?本地文件直接用FileInputStream即可。
jspsmartupload需要在提交的form表单中添加一个属性,具体内容忘了=。=

热点内容
对等局域网与客户机服务器有什么不同 发布:2024-05-05 07:51:15 浏览:174
win7Linux修复linux 发布:2024-05-05 07:47:17 浏览:60
oracle批处理脚本 发布:2024-05-05 07:32:20 浏览:392
linuxftp响应慢 发布:2024-05-05 07:23:03 浏览:802
sql查询所有字段 发布:2024-05-05 07:22:07 浏览:671
电脑的存储符号 发布:2024-05-05 07:15:21 浏览:131
sql转换成数据类型int时失败 发布:2024-05-05 06:29:21 浏览:827
苹果手机视频怎么加密 发布:2024-05-05 06:22:08 浏览:919
java反编译工具使用方法 发布:2024-05-05 06:00:38 浏览:218
恋人源码 发布:2024-05-05 05:53:33 浏览:167