用java实现文件的上传与下载1.下载简单,无非是把服务器上的文件或者数据库中的BLob(或其他二进制型),用流读出来,然后写到客户端即可,要注意 ContentType 。
2.上传,可以用Apache Commons Upload等开源工具,或者自己写:
form要用enctype="multipart/form-data"
然后服务器端也是用IO把客户端提交的文件流读入,然后写到服务器的文件系统或者数据库里 。不同的数据库对Lob字段操作可能有所不同,建议用Hibernate,JPA等成熟的ORM框架,可以不考虑数据库细节 。
用Java的三大框架实现文件的上传下载,求代码啊 , 最好是分为action,service,servpackage cn.itcast.struts2.demo1;
import java.io.File;
import org.apache.commons.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.commons.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;
【java上传下载代码 java文件上传下载项目】 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; iupload.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文件上传下载的主要代码 , 非网页的,最好关键地方能有说明利用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; iupload.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 文件上传下载的代码FileInputStream fin = new FileInputStream(new File("java上传下载代码你的文件地址"));
OutputStream out = 你的目标流地址java上传下载代码,可以是Socket的Output流java上传下载代码,也可以是http的Output流java上传下载代码,等等
byte[] b = new byte[65535]; // 一次读取多少字节
int read = -1;
while(-1 != (read = fin.read(b))){
out.write(b,0,read);
}
怎么用Java代码实现上传下载附件int id = (Integer) request.getSession().getAttribute("id");
DiskFileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload sfu = new ServletFileUpload(factory);
sfu.setFileSizeMax(1024 * 1024);//1M
ListFileItem items = sfu.parseRequest(request);
for (int i = 0; iitems.size(); i) {
FileItem item = items.get(i);
if (!item.isFormField()) {
ServletContext sctx = getServletContext();
String picurl = sctx.getRealPath("upload");
String fileName = item.getName();
fileName = fileName.substring(fileName
.lastIndexOf("\\")1);
PicDao picDao = new PicDaoImpl();
System.err.println(id);
picDao.savePic(id, fileName);
File file = new File(picurl"/pic_"
StringUtils.leftPad(id"", 5, '0')"/"
fileName);
item.write(file);
}
}
response.sendRedirect("userDetail.do?id="id);
java上传文件代码public class FileUpLoadextends 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上传下载代码的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于java文件上传下载项目、java上传下载代码的信息别忘了在本站进行查找喔 。
推荐阅读
- 游戏养成类小说甜文,玩游戏养成的小说
- pg数据库plantable,pg数据库copy命令导入表数据
- 《python项目开发实战》,python项目设计与开发
- 如何给word表格绘制斜线,怎样在word表格中绘制斜线
- java动态加载代码 java 动态加载jar包
- 手机游戏ios角色扮演,手机游戏ios角色扮演软件
- 头条如何加入创作计划推广,头条如何加入创作者计划
- linux命令gre linux命令grep和find
- 哪些需要做redis缓存,什么情况下用redis