上传下载文件java代码 上传下载文件java代码错误
用java实现文件的上传与下载
1.下载简单,无非是把服务器上的文件或者数据库中的BLob(或其他二进制型),用流读出来,然后写到客户端即可,要注意 ContentType。
成都创新互联公司服务项目包括嘉祥网站建设、嘉祥网站制作、嘉祥网页制作以及嘉祥网络营销策划等。多年来,我们专注于互联网行业,利用自身积累的技术优势、行业经验、深度合作伙伴关系等,向广大中小型企业、政府机构等提供互联网行业的解决方案,嘉祥网站推广取得了明显的社会效益与经济效益。目前,我们服务的客户以成都为中心已经辐射到嘉祥省份的部分城市,未来相信会继续扩大服务区域并继续获得客户的支持与信任!
2.上传,可以用Apache Commons Upload等开源工具,或者自己写:
form要用enctype="multipart/form-data"
然后服务器端也是用IO把客户端提交的文件流读入,然后写到服务器的文件系统或者数据库里。不同的数据库对Lob字段操作可能有所不同,建议用Hibernate,JPA等成熟的ORM框架,可以不考虑数据库细节。
用Java的三大框架实现文件的上传下载,求代码啊,最好是分为action,service,serv
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;
/**
* 完成文件上传 (不是解析上传内容,因为上传内容 由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;
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实现文件的上传和下载
用输出流 接受 一个下载地址的网络流
然后将这个输出流 保存到本地一个文件 后缀与下载地址的后缀相同··
上传的话 将某个文件流 转成字节流 上传到某个webservice方法里
-------要代码来代码
URL url=new URL("");
URLConnection uc=url.openConnection();
InputStream in=uc.getInputStream();
BufferedInputStream bis=new BufferedInputStream(in);
FileOutputStream ft=new FileOutputStream("E://1.rar");
这是下载 上传太麻烦就不给写了
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文件上传下载的主要代码,非网页的,最好关键地方能有说明
利用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 action中如何 上传 下载文件??
/**
上传文件
*/
public class FileAction extends Action {
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
try {
FileForm fileform = (FileForm) form;
//取得请求的文件集合
Hashtable hash = fileform.getMultipartRequestHandler().getFileElements();
//得到hashtable的枚举值
Enumeration enu = hash.elements();
//如果该枚举值包含有其它的文件
while(enu.hasMoreElements()) {
//得到文件
FormFile file = (FormFile) enu.nextElement();
System.out.println(file);
add(file);
}
return mapping.findForward("yes");
} catch (Exception e) {
e.printStackTrace();
}
return super.execute(mapping, form, request, response);
}
public void add(FormFile file){
try {
//取得写文件的目录
String url=servlet.getServletContext().getRealPath("upload");
File f1=new File(url);
if(!f1.exists()){//如果文件目录不存在
f1.mkdirs();//创建目录
}
String fileName=file.getFileName();
//创建一个文件输入流
InputStream is=file.getInputStream();
OutputStream out=new FileOutputStream(url+"/"+fileName);
int byteRead=0;
byte[] by=new byte[8192];
while((byteRead=is.read(by, 0, 8192))!=-1){
out.write(by, 0, byteRead);
}
out.close();
is.close();
file.destroy();
} catch (Exception e) {
e.printStackTrace();
}
}
}
/**
下载文件
*/
页面一开始进去action,action负责把file文件夹下的所有文件读入一个ArrayList中
Action代码如下:
ArrayList list = new ArrayList();
String path=request.getRealPath("/")+"file";
String FullPath;
//System.out.println(path);
myDir=new File(path);
list.clear();
contents=myDir.listFiles();
for(int i=0;icontents.length;i++){
FullPath=contents.getName();
list.add(FullPath);
//System.out.println(FullPath);
}
request.setAttribute("list",list);
ActionForward forward=new ActionForward("/download.jsp");
return forward;
然后进入download.jsp中,这个页面主要负责把所有文件显示,并提供下载连接,代码如下:
%@ page language="java" contentType="text/html;charset=GBK" import="java.util.ArrayList"%
head
/style
/head
body
%ArrayList list=(ArrayList)request.getAttribute("list");
for(int i=0;ilist.size();i++)
{
String a=java.net.URLEncoder.encode((String)list.get(i));
out.print("a href=./loaded.do?name="+a+""+list.get(i)+"/abr");
}
%
/body
/html
注意,下划线画中的代码的作用,就是解决问题的所在。
接下来可以直接传入到loadedaction中,也可以通过一个form,我演示的是通过一个form
Form代码如下
package org.aeolus.struts.form;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
public class LoadForm extends ActionForm {
/*
*Generated Methods
*/
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
接下来就是action的代码
LoadForm doc=(LoadForm)form;
String docName = new String(doc.getName().getBytes("8859_1"));
File f;
if(docName!=""){
String docFullPath=request.getRealPath("/");
f = new File(docFullPath+"file\\"+docName);
response.reset();
response.setContentType("application/x-msdownload;charset=GBK");
System.out.print(response.getContentType());
response.setCharacterEncoding("UTF-8");
docName=java.net.URLEncoder.encode(docName,"UTF-8");
response.setHeader("Content-Disposition", "attachment;filename=" +new String(docName.getBytes("UTF-8"),"GBK"));
BufferedInputStream br = new BufferedInputStream(new FileInputStream(f));
byte[] buf = new byte[1024];
int len = 0;
OutputStream out = response.getOutputStream();
while((len = br.read(buf)) 0)
out.write(buf,0,len);
out.close();
response.wait();
ActionForward forward=new ActionForward("/download.jsp");
return forward; }
return null;
注意,下划线画中的代码的作用,就是解决问题的所在。说明一下:
response.setCharacterEncoding("UTF-8");
docName=java.net.URLEncoder.encode(docName,"UTF-8");
response.setHeader("Content-Disposition", "attachment;filename=" +new String(docName.getBytes("UTF-8"),"GBK"));
如果不这样做你将要下载的文件名是乱码。
网站栏目:上传下载文件java代码 上传下载文件java代码错误
当前URL:http://azwzsj.com/article/hgcsoo.html