1.下載簡單,無非是把服務(wù)器上的文件或者數(shù)據(jù)庫中的BLob(或其他二進制型),用流讀出來,然后寫到客戶端即可,要注意 ContentType。
10年積累的成都做網(wǎng)站、網(wǎng)站建設(shè)、外貿(mào)營銷網(wǎng)站建設(shè)經(jīng)驗,可以快速應(yīng)對客戶對網(wǎng)站的新想法和需求。提供各種問題對應(yīng)的解決方案。讓選擇我們的客戶得到更好、更有力的網(wǎng)絡(luò)服務(wù)。我雖然不認識你,你也不認識我。但先網(wǎng)站制作后付款的網(wǎng)站建設(shè)流程,更有上林免費網(wǎng)站建設(shè)讓你可以放心的選擇與我們合作。
2.上傳,可以用Apache Commons Upload等開源工具,或者自己寫:
form要用enctype="multipart/form-data"
然后服務(wù)器端也是用IO把客戶端提交的文件流讀入,然后寫到服務(wù)器的文件系統(tǒng)或者數(shù)據(jù)庫里。不同的數(shù)據(jù)庫對Lob字段操作可能有所不同,建議用Hibernate,JPA等成熟的ORM框架,可以不考慮數(shù)據(jù)庫細節(jié)。
利用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?{
/**
*?用于上傳的變量
*/
//封裝該文件域?qū)?yīng)的文件內(nèi)容
private?File[]?upload;
//封裝該文件域?qū)?yīng)的文件的文件名
private?String[]?uploadFileName;
//封裝該文件域?qū)?yīng)的文件的文件類型
private?String[]?uploadContentType;
/**
*?用于下載的變量
*/
private?String[]?fileNames;
private?String?fileName;
/**
*?設(shè)置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(){
//設(shè)置文件上傳到的位置
String?path?=?ServletActionContext.getServletContext().getRealPath("/file");
//設(shè)置文件目標
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方便多了
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;
/**
* 完成文件上傳 (不是解析上傳內(nèi)容,因為上傳內(nèi)容 由fileUpload攔截器負責解析)
*
* @author seawind
*
*/
public class UploadAction extends ActionSupport {
// 接收上傳內(nèi)容
// 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;
}
// 將上傳文件 保存到服務(wù)器端
// 源文件 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 {
// 接收多文件上傳參數(shù),提供數(shù)組接收就可以了
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++) {
// 循環(huán)完成上傳
File srcFile = upload[i];
String filename = uploadFileName[i];
// 定義目標文件
File destFile = new File(ServletActionContext.getServletContext()
.getRealPath("/upload" + "/" + filename));
FileUtils.copyFile(srcFile, destFile);
}
return NONE;
}
}
FileInputStream fin = new FileInputStream(new File("你的文件地址"));
OutputStream out = 你的目標流地址,可以是Socket的Output流,也可以是http的Output流,等等
byte[] b = new byte[65535]; // 一次讀取多少字節(jié)
int read = -1;
while(-1 != (read = fin.read(b))){
out.write(b,0,read);
}