當前位置:ag真人国际官网-ag旗舰厅官方网站 » 文件管理 » jsp實現文件上傳

jsp實現文件上傳-ag真人国际官网

發布時間: 2024-07-05 12:19:57

a. 用jsp、java實現上傳圖片,保存到資料庫,從資料庫中提取,顯示到頁面 這四步 我想問第一步,怎麼上傳圖片

用jspsmartupload組件來實現,用jsp servlet在servlet里實現的代碼:

printwriter out = response.getwriter();
int count = 0;
// 實例化上傳控制項對象
smartupload su = new smartupload();
// 初始化操作
su.initialize(config, request, response);

// 設置上傳文件最大位元組數
su.settotalmaxfilesize(100000);

//
try {
//禁止上傳指定擴展名的文件
su.setdeniedfileslist("ext,bat,jsp");
} catch (sqlexception e1) {
e1.printstacktrace();
}

try {
// 上傳文件到伺服器
su.upload();

file fileup = new file(request.getrealpath("upload"));
if(!fileup.exists()){
// 創建目錄
fileup.mkdir();
}
// 處理多個文件的上傳
for(int i = 0;i < su.getfiles().getcount();i ){
com.jspsmart.upload.file file = su.getfiles().getfile(i);
if(!file.ismissing()){ // 如果文件有效
// 保存文件到指定上傳目錄
file.saveas("/upload/new." file.getfileext(), su.save_virtual);
count = su.save("/upload");
}
}

} catch (smartuploadexception e) {

e.printstacktrace();
}
out.println(count "file(s) uploaded");

如果你對這個上傳組件不了解,最好是先去查查用法。。。

如果對您有幫助,請記得採納為滿意答案,謝謝!祝您生活愉快!

vaela

b. jsp servlet實現文件上傳與下載源碼

上傳:
需要導入兩個包:commons-fileupload-1.2.1.jar,commons-io-1.4.jar
import java.io.file;
import java.io.ioexception;
import java.util.list;

import javax.servlet.servletexception;
import javax.servlet.http.httpservlet;
import javax.servlet.http.httpservletrequest;
import javax.servlet.http.httpservletresponse;

import org.apache.commons.fileupload.fileitem;
import org.apache.commons.fileupload.disk.diskfileitemfactory;
import org.apache.commons.fileupload.servlet.servletfileupload;

/**
* 上傳附件
* @author new
*
*/
public class uploadannexservlet extends httpservlet {

private static string path = "";

public void doget(httpservletrequest request, httpservletresponse response)
throws servletexception, ioexception {

dopost(request, response);
}

/*
* post處理
* (non-javadoc)
* @see javax.servlet.http.httpservlet#dopost(javax.servlet.http.httpservletrequest, javax.servlet.http.httpservletresponse)
*/
public void dopost(httpservletrequest request, httpservletresponse response)
throws servletexception, ioexception {

path = this.getservletcontext().getrealpath("/upload");

try {
diskfileitemfactory factory = new diskfileitemfactory();
servletfileupload up = new servletfileupload(factory);
list ls = up.parserequest(request);

for (fileitem fileitem : ls) {
if (fileitem.isformfield()) {
string fieldname = fileitem.getfieldname();
//getname()返回的是文件名字 普通域沒有文件 返回null
// string name = fileitem.getname();
string content = fileitem.getstring("gbk");
request.setattribute(fieldname, content);
} else {

string nm = fileitem.getname().substring(
fileitem.getname().lastindexof("\\") 1);
file mkr = new file(path, nm);
if (mkr.createnewfile()) {
fileitem.write(mkr);//非常方便的方法
}
request.setattribute("result", "上傳文件成功!");
}
}
} catch (exception e) {
e.printstacktrace();
request.setattribute("result", "上傳失敗,請查找原因,重新再試!");
}
request.getrequestdispatcher("/pages/admin/annex-manager.jsp").forward(
request, response);
}

}

下載(i/o流)無需導包:
import java.io.ioexception;
import java.net.urlencoder;

import javax.servlet.servletexception;
import javax.servlet.http.httpservlet;
import javax.servlet.http.httpservletrequest;
import javax.servlet.http.httpservletresponse;

/**
* 下載文件
* @author
*
*/
public class downloadfilesservlet extends httpservlet {

/**
*
*/
private static final long serialversionuid = 8594448765428224944l;

public void doget(httpservletrequest request, httpservletresponse response)
throws servletexception, ioexception {

dopost(request, response);
}

/*
* 處理請求
* (non-javadoc)
* @see javax.servlet.http.httpservlet#dopost(javax.servlet.http.httpservletrequest, javax.servlet.http.httpservletresponse)
*/
public void dopost(httpservletrequest request, httpservletresponse response)
throws servletexception, ioexception {

string name = request.getparameter("filename");

system.out.print("dddddddddd:" name);
// web絕對路徑
string path = request.getsession().getservletcontext().getrealpath("/");
string savepath = path "upload";

// 設置為下載application/x-download
response.setcontenttype("application/x-download");
// 即將下載的文件在伺服器上的絕對路徑
string filenamedownload = savepath "/" name;
// 下載文件時顯示的文件保存名稱
string filenamedisplay = name;
// 中文編碼轉換
filenamedisplay = urlencoder.encode(filenamedisplay, "utf-8");
response.addheader("content-disposition", "attachment;filename="
filenamedisplay);
try {
java.io.outputstream os = response.getoutputstream();
java.io.fileinputstream fis = new java.io.fileinputstream(
filenamedownload);
byte[] b = new byte[1024];
int i = 0;
while ((i = fis.read(b)) > 0) {
os.write(b, 0, i);
}
fis.close();
os.flush();
os.close();
} catch (exception e) {

}

}

}

c. jsp上傳圖片,最好完整代碼。100分!

upfile.jsp 文件代碼如下:









uploadimage.jsp
文件代碼如下:
uploadimage.jsp
文件代碼如下:view plain to clipboardprint?
<%@ page language="java"  pageencoding="gb2312"%>     
<%@ page import="java.io.*,java.awt.image,java.awt.image.*,com.sun.image.codec.jpeg.*,java.sql.*,com.jspsmart.upload.*,java.util.*"%>
<%@ page import="mainclass.*" %>


my jsp 'uploadimage.jsp' starting page


<%
smartupload sma=new smartupload();
long file_max_size=4000000;
string filename1="",ext="",testvar="";
string url="uploadfiles/";
sma.initialize(pagecontext);
try
{
sma.setallowedfileslist("jpg,gif");
sma.upload();
}catch(exception e){
%>

<%
}
try{
com.jspsmart.upload.file myf=sma.getfiles().getfile(0);
if(myf.ismissing()){
%>

<%
}else{
ext=myf.getfileext();
int file_size=myf.getsize();
string saveurl="";
if(file_size < file_max_size){
calendar cal=calendar.getinstance();
string filename=string.valueof(cal.gettimeinmillis());
saveurl=request.getrealpath("/") url;
saveurl =filename "." ext;
myf.saveas(saveurl,sma.save_physical);
myclass mc=new myclass(request.getrealpath("data/data.mdb"));
mc.executeinsert("insert into [path] values('uploadfiles/" filename "." ext "')");
out.println("圖片上傳成功!");
response.sendredirect("showimg.jsp");
}
}
}catch(exception e){
e.printstacktrace();
}
%>




本文來自: it知道網(http://www.itwis.com) 詳細出處參考:http://www.itwis.com/html/java/jsp/20080916/2409.html

d. jsp 文件上傳和下載

1.jsp頁面







2.action
//單個文件上傳可以用 file files,string filesfilename,string filescontenttype
//名稱要與jsp中的name相同(三個變數都要生成get,set)
private file[] files;
// 要以file[]變數名開頭
private string[] filesfilename;
// 要以file[]變數名開頭
private string[] filescontenttype;

private servletcontext servletcontext;

//action調用的上傳文件方法
public string execute() {
servletcontext servletcontext = servletactioncontext.getservletcontext();
string datadir = servletcontext.getrealpath("/file/upload");
system.out.println(datadir);
for (int i = 0; i < files.length; i ) {
file savefile = new file(datadir, filesfilename[i]);
files[i].renameto(savefile);
}
return "success";
}
3.配置上傳文件臨時文件夾(在struts.xml中配置)

文件下載
1.下載的

2.struts.xml配置






application/octet-stream

inputstream

attachment;filename="${filename}"

4096



3.action
//action調用的下載文件方法
public string down() {
return "down";
}

//獲得下載文件的內容,可以直接讀入一個物理文件或從資料庫中獲取內容
public inputstream getinputstream() throws exception {
string dir = servletcontext.getrealpath("/file/upload");
file file = new file(dir, "icon.png");
if (file.exists()) {
//下載文件
return new fileinputstream(file);

//和 servlet 中不一樣,這里我們不需對輸出的中文轉碼為 iso8859-1
//將內容(struts2 文件下載測試)直接寫入文件,下載的文件名必須是文本(txt)類型
//return new bytearrayinputstream("struts2 文件下載測試".getbytes());
}
return null;
}

// 對於配置中的 ${filename}, 獲得下載保存時的文件名
public string getfilename() {
string filename ="圖標.png";
try {
// 中文文件名也是需要轉碼為 iso8859-1,否則亂碼
return new string(filename.getbytes(), "iso8859-1");
} catch (unsupportedencodingexception e) {
return "icon.png";
}
}

e. jsp 如何實現文件上傳和下載功能

上傳:

myjspform mf = (myjspform) form;// todo auto-generated method stub

formfile fname=mf.getfname();

byte [] fn = fname.getfiledata();

outputstream out = new fileoutputstream("d:\" fname.getfilename());

date date = new date();

string title = fname.getfilename();

string url = "d:\" fname.getfilename();

upload ul = new upload();

ul.setdate(date);

ul.settitle(title);

ul.set;

uploaddao uld = new uploaddao();

uld.save(ul);

out.write(fn);

out.close();

下載:

downloadform downloadform = (downloadform)form;

string fname = request.getparameter("furl");

fileinputstream fi = new fileinputstream(fname);

byte[] bt = new byte[fi.available()];

fi.read(bt);

//設置文件是下載還是打開以及打開的方式msdownload表示下載;設置字湖集,//主要是解決文件中的中文信息

response.setcontenttype("application/msdownload;charset=gbk");

//文件下載後的默認保存名及打開方式

string contentdisposition = "attachment; filename=" "java.txt";

response.setheader("content-disposition",contentdisposition);

//設置下載長度

response.setcontentlength(bt.length);

servletoutputstream sos = response.getoutputstream();

sos.write(bt);

return null;

f. java編程:怎麼用jsp(javabean)上傳一張圖片到伺服器的指定文件夾呢

先導smartupload jar包!在寫form表單enctype和method別寫錯了!
寫一個簡單的吧!
<%page import="com.jspsmart.upload.*"%>
<%
smartupload su=new smartupload ();//初始化smartupload對象
try{ //捕獲他可能出現的異常
su.upload();//執行上傳
}catch(exception ex){
ex.printstacktrace;
}
file file=su.getfile().getfile(0); //(得到單個的上傳文件的信息)這里得到的file對象是你到的jar包里的com.jspsmart.upload.file類型 別寫成io 裡面的file了
string filepath="upload\\"; //在這之前要在你所建項目的目錄下單建一個upload文件夾
filepath =file.getfilename();
file.saveas(filepath,smartupload.save-virtual);
不知道是否建了與它相對應的資料庫表啊?
不懂得再玩吧!

%>

熱點內容
sqlserver如何切換主備伺服器 發布:2024-07-17 16:23:02 瀏覽:298
mc18伺服器ip 發布:2024-07-17 16:23:02 瀏覽:378
仙境傳說手游腳本 發布:2024-07-17 16:09:24 瀏覽:690
matlab命令窗口和新建腳本 發布:2024-07-17 15:51:26 瀏覽:374
建ftp文件夾 發布:2024-07-17 15:51:26 瀏覽:954
魔獸撿物腳本 發布:2024-07-17 15:27:56 瀏覽:129
開發ip伺服器 發布:2024-07-17 15:24:42 瀏覽:387
安卓系統視頻製作哪個好用 發布:2024-07-17 15:10:47 瀏覽:210
androidapk結構 發布:2024-07-17 15:10:43 瀏覽:945
c語言指針的例子 發布:2024-07-17 15:08:01 瀏覽:768
网站地图