资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

java中上传下载代码,java文件上传下载实现

java上传文件代码

public class FileUpLoad extends ActionSupport{

创新互联建站专注于宜春网站建设服务及定制,我们拥有丰富的企业做网站经验。 热诚为您提供宜春营销型网站建设,宜春网站制作、宜春网页设计、宜春网站官网定制、成都微信小程序服务,打造宜春网络公司原创品牌,更为您提供宜春网站排名全网营销落地服务。

//"多文件上传就用list就可以了private ListFile 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 文件上传下载的代码

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实现文件的上传与下载

1.下载简单,无非是把服务器上的文件或者数据库中的BLob(或其他二进制型),用流读出来,然后写到客户端即可,要注意 ContentType。

2.上传,可以用Apache Commons Upload等开源工具,或者自己写:

form要用enctype="multipart/form-data"

然后服务器端也是用IO把客户端提交的文件流读入,然后写到服务器的文件系统或者数据库里。不同的数据库对Lob字段操作可能有所不同,建议用Hibernate,JPA等成熟的ORM框架,可以不考虑数据库细节。

求一java文件上传下载的主要代码,非网页的,最好关键地方能有说明

利用struts2的上传下载

package com.java.action;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.IOException;

import java.io.InputStream;

import java.io.UnsupportedEncodingException;

import java.net.URLEncoder;

import org.apache.commons.io.FileUtils;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;

public class FileAction extends ActionSupport {

/**

* 用于上传的变量

*/

//封装该文件域对应的文件内容

private File[] upload;

//封装该文件域对应的文件的文件名

private String[] uploadFileName;

//封装该文件域对应的文件的文件类型

private String[] uploadContentType;

/**

* 用于下载的变量

*/

private String[] fileNames;

private String fileName;

/**

* 设置getter和setter

* @return

*/

public String[] getFileNames() {

return fileNames;

}

public File[] getUpload() {

return upload;

}

public void setUpload(File[] upload) {

this.upload = upload;

}

public String[] getUploadFileName() {

return uploadFileName;

}

public void setUploadFileName(String[] uploadFileName) {

this.uploadFileName = uploadFileName;

}

public String[] getUploadContentType() {

return uploadContentType;

}

public void setUploadContentType(String[] uploadContentType) {

this.uploadContentType = uploadContentType;

}

public void setFileNames(String[] fileNames) {

this.fileNames = fileNames;

}

public String getFileName() {

return fileName;

}

public void setFileName(String fileName) {

this.fileName = fileName;

}

/**

* 用于上传文件的方法

* @return

*/

public String upload(){

//设置文件上传到的位置

String path = ServletActionContext.getServletContext().getRealPath("/file");

//设置文件目标

try {

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

File target = new File(path, uploadFileName[i]);

FileUtils.copyFile(upload[i], target);

}

return SUCCESS;

} catch (IOException e) {

e.printStackTrace();

}

return INPUT;

}

/**

* 得到所有上传的文件的名称

* @return

*/

public String fileList(){

String path = ServletActionContext.getServletContext().getRealPath("/file");

fileNames = new File(path).list();

return SUCCESS;

}

/**

* 用于下载文件的方法

* @return

*/

public InputStream getInputStream(){

if(fileName==null || fileName.isEmpty()) return null;

String path = ServletActionContext.getServletContext().getRealPath("/file");

try {

return new FileInputStream(new File(path,fileName));

} catch (FileNotFoundException e) {

e.printStackTrace();

}

return null;

}

public String getContentDisposition(){

try {

if(fileName==null||fileName.isEmpty()){

return "inline";

}

return "attachment;filename="+URLEncoder.encode(fileName, "UTF-8");

} catch (UnsupportedEncodingException e) {

e.printStackTrace();

}

return "inline";

}

}

比java的io方便多了


本文名称:java中上传下载代码,java文件上传下载实现
当前URL:http://cdkjz.cn/article/hchohi.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

大客户专线   成都:13518219792   座机:028-86922220