189 8069 5689

文件上传下载java代码 javaweb上传下载文件

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

// 这是我写的一个方法,里面只需要传两个参数就OK了,在任何地方调用此方法都可以文件上传

成都创新互联公司主营双塔网站建设的网络公司,主营网站建设方案,成都App制作,双塔h5微信平台小程序开发搭建,双塔网站营销推广欢迎双塔等地区企业咨询

/**

* 上传文件

* @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 文件上传下载的代码

FileInputStream fin = new FileInputStream(new File("你的文件地址"));

OutputStream out = 你的目标流地址,可以是Socket的Output流,也可以是http的Output流,等等

byte[] b = new byte[65535]; // 一次读取多少字节

int read = -1;

while(-1 != (read = fin.read(b))){

out.write(b,0,read);

}

用Java的三大框架实现文件的上传下载,求代码啊,最好是分为action,service,serv

package cn.itcast.struts2.demo1;

import java.io.File;

import org.apache点抗 mons.io.FileUtils;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;

/**

* 完成文件上传 (不是解析上传内容,因为上传内容 由fileUpload拦截器负责解析)

*

* @author seawind

*

*/

public class UploadAction extends ActionSupport {

// 接收上传内容

// input type="file" name="upload" /

private File upload; // 这里变量名 和 页面表单元素 name 属性一致

private String uploadContentType;

private String uploadFileName;

public void setUpload(File upload) {

this.upload = upload;

}

public void setUploadContentType(String uploadContentType) {

this.uploadContentType = uploadContentType;

}

public void setUploadFileName(String uploadFileName) {

this.uploadFileName = uploadFileName;

}

@Override

public String execute() throws Exception {

if (upload == null) { // 通过xml配置 required校验器 完成校验

// 没有上传文件

return NONE;

}

// 将上传文件 保存到服务器端

// 源文件 upload

// 目标文件

File destFile = new File(ServletActionContext.getServletContext()

.getRealPath("/upload") + "/" + uploadFileName);

// 文件复制 使用commons-io包 提供 工具类

FileUtils.copyFile(upload, destFile);

return NONE;

}

}

多文件上传

package cn.itcast.struts2.demo1;

import java.io.File;

import org.apache点抗 mons.io.FileUtils;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;

/**

* 支持多文件上传

*

* @author seawind

*

*/

public class MultiUploadAction extends ActionSupport {

// 接收多文件上传参数,提供数组接收就可以了

private File[] upload;

private String[] uploadContentType;

private String[] uploadFileName;

public void setUpload(File[] upload) {

this.upload = upload;

}

public void setUploadContentType(String[] uploadContentType) {

this.uploadContentType = uploadContentType;

}

public void setUploadFileName(String[] uploadFileName) {

this.uploadFileName = uploadFileName;

}

@Override

public String execute() throws Exception {

for (int i = 0; i upload.length; i++) {

// 循环完成上传

File srcFile = upload[i];

String filename = uploadFileName[i];

// 定义目标文件

File destFile = new File(ServletActionContext.getServletContext()

.getRealPath("/upload" + "/" + filename));

FileUtils.copyFile(srcFile, destFile);

}

return NONE;

}

}


本文标题:文件上传下载java代码 javaweb上传下载文件
标题链接:http://cdxtjz.cn/article/ddgosgj.html

其他资讯