先放好jspsmartupload包

成都創(chuàng)新互聯(lián)公司主營(yíng)豐順網(wǎng)站建設(shè)的網(wǎng)絡(luò)公司,主營(yíng)網(wǎng)站建設(shè)方案,成都App定制開發(fā),豐順h5微信小程序搭建,豐順網(wǎng)站營(yíng)銷推廣歡迎豐順等地區(qū)企業(yè)咨詢
%@ page contentType="text/html;charset=GB2312" %
%@ page language="java" import="com.jspsmart.upload.*"%
HTML
head
title文件上傳處理頁面/title
/head
BODY
p align="center"jspSmartUpload組件應(yīng)用之員工檔案/p
HR
%
// 新建一個(gè)SmartUpload對(duì)象
SmartUpload su = new SmartUpload();
// 上傳初始化
su.initialize(pageContext);
// 設(shè)定允許上傳的文件(通過擴(kuò)展名限制),僅允許jpg,bmp,gif文件。
su.setAllowedFilesList("jpg,bmp,gif");
// 上傳文件
su.upload();
// 將上傳文件全部保存到指定目錄
su.save("/upload");
%
p align="center"
table CELLSPACING="0" CELLPADDING="3" BORDER="1" WIDTH="474"
tr
td width="150"div align="left"psmallfont face="Verdana"姓名:? /font/small/td
td width="324"smallfont face="Verdana"
%=su.getRequest().getParameter("username")%
br/font/small/td
/tr
tr
tdsmallfont face="Verdana"照片:? /font/small/td
tdsmallimage src="%="upload/"+su.getFiLes().getFile(0).getFileName()%"/td
/tr
tr
tdsmallfont face="Verdana"年齡: /font/small/td
tdsmallfont face="Verdana"%=su.getRequest().getParameter("age")%/font/small/td
/tr
tr
tdsmallfont face="Verdana"工作單位: /font/small/td
tdsmallfont face="Verdana"%=su.getRequest().getParameter("department")%/font/small/td
/tr
tr
td colspan="2" width="474"div align="center"centerpsmallfont face="Verdana"
input TYPE="Submit" value="提交" /font/small/td
/tr
/table
/p
/BODY
/HTML
private File file;
private String fileFileName;
private String picture;
//都有g(shù)etter 和 setter
InputStream is = new FileInputStream(file);
//引入一個(gè)IO流的輸入流
String root = ServletActionContext.getRequest()
.getRealPath("/bookpicture");
//通過REQUEST來得到相對(duì)地址,并在后面加上/bookpicture
File f = new File(root, this.getFileFileName());
//定義一個(gè)FILE文件,第一個(gè)參數(shù)是文件的路徑,第二個(gè)是文件的名字
picture="."+"\\"+"bookpicture"+"\\"+this.getFileFileName();
//為PICTURE字符串賦值,/地址/文件名
System.out.println
("======picture====="+picture);
//從控制臺(tái)輸出Picture
OutputStream os = new FileOutputStream(f);
//第一個(gè)文件的輸出流
byte[] buffer = new byte[1024];
//定義一個(gè)bufer的字符串,長(zhǎng)度為1024
int len = 0;
while ((len = is.read(buffer)) 0) {
//如果從制定文件中讀取到的信息為結(jié)束就繼續(xù)循環(huán)
os.write(buffer, 0, len);
//將文件讀出的內(nèi)容寫入到指定的文件中
}
我有一段上傳圖片的代碼,并且可以根據(jù)實(shí)際,按月或按天等,生成存放圖片的文件夾
首先在JSP上放一個(gè)FILE的標(biāo)簽這些我都不說了,你也一定明白,我直接把處理過程給你發(fā)過去
我把其中存到數(shù)據(jù)庫中的內(nèi)容刪除了,你改一下就能用
/**
*
* 上傳圖片
* @param servlet
* @param request
* @param response
* @return
* @throws Exception
*/
//這里我是同步上傳的,你隨意
public synchronized String importPic(HttpServlet servlet, HttpServletRequest request,HttpServletResponse response) throws Exception {
SimpleDate()Format formatDate() = new SimpleDate()Format("yyyyMM");
Date nowtime=new Date();
String formatnowtime=formatDate.format(nowtime);
File root = new File(request.getRealPath("/")+"uploadfile/images/"+formatnowtime+"/"); //應(yīng)保證在根目錄中有此目錄的存在 如果沒有,下面則上創(chuàng)建新的文件夾
if(!root.isDirectory())
{
System.out.println("創(chuàng)建新文件夾成功"+formatnowtime);
root.mkdir();
}
int returnflag = 0;
SmartUpload mySmartUpload =new SmartUpload();
int file_size_max=1024000;
String ext="";
String url="uploadfile/images/"+formatnowtime+"/";
// 只允許上載此類文件
try{
// 初始化
mySmartUpload.initialize(servlet.getServletConfig(),request,response);
mySmartUpload.setAllowedFilesList("jpg,gif,bmp,jpeg,png,JPG");
// 上載文件
mySmartUpload.upload();
} catch (Exception e){
response.sendRedirect()//返回頁面
}
com.jspsmart.upload.File myFile = mySmartUpload.getFiles().getFile(0);
if (myFile.isMissing()){ //沒有選擇圖片做提示!
returnflag = 3;
}else{
String myFileName=myFile.getFileName(); //取得上載的文件的文件名
ext= myFile.getFileExt(); //取得后綴名
if(ext.equals("jpg")||ext.equals("gif")||ext.equals("bmp")||ext.equals("jpeg")||ext.equals("png")||ext.equals("JPG")){ //jpeg,png不能上傳!)
int file_size=myFile.getSize(); //取得文件的大小
String saveurl="";
if(file_sizefile_size_max){
try{
//我上面說到,把操作數(shù)據(jù)庫的代友刪除了,這里就應(yīng)該是判斷,你的圖片是不是已經(jīng)存在了,存在要怎么處理,不存在要怎么處了,就是你的事了 }
//更改文件名,取得當(dāng)前上傳時(shí)間的毫秒數(shù)值
Calendar calendar = Calendar.getInstance();
//String filename = String.valueOf(calendar.getTimeInMillis());
String did = contractBean.getMaxSeq("MULTIMEDIA_SEQ");
String filename = did;
String flag = "0";
String path = request.getRealPath("/")+url;
String ename = myFile.getFileExt();
//.toLowerCase()轉(zhuǎn)換大小寫
saveurl=request.getRealPath("/")+url;
saveurl+=filename+"."+ext; //保存路徑
myFile.saveAs(saveurl,mySmartUpload.SAVE_PHYSICAL);
//將圖片信息插入到數(shù)據(jù)庫中
// ------上傳完成,開始生成縮略圖-----
java.io.File file = new java.io.File(saveurl); //讀入剛才上傳的文件
String newurl=request.getRealPath("/")+url+filename+"_min."+ext; //新的縮略圖保存地址
Image src = javax.imageio.ImageIO.read(file); //構(gòu)造Image對(duì)象
float tagsize=200;
int old_w=src.getWidth(null);
int old_h=src.getHeight(null);
int new_w=0;
int new_h=0;
int tempsize;
float tempdouble;
if(old_wold_h){
tempdouble=old_w/tagsize;
}else{
tempdouble=old_h/tagsize;
}
// new_w=Math.round(old_w/tempdouble);
// new_h=Math.round(old_h/tempdouble);//計(jì)算新圖長(zhǎng)寬
new_w=150;
new_h=110;//計(jì)算新圖長(zhǎng)寬
BufferedImage tag = new BufferedImage(new_w,new_h,BufferedImage.TYPE_INT_RGB);
tag.getGraphics().drawImage(src,0,0,new_w,new_h,null); //繪制縮小后的圖
FileOutputStream newimage=new FileOutputStream(newurl); //輸出到文件流
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(newimage);
encoder.encode(tag); //近JPEG編碼
newimage.close();
returnflag = 1;
}else{
returnflag = 0;
System.out.println("('上傳文件大小不能超過"+(file_size_max/1000)+"K');");
}
}else{
returnflag = 2;
}
}
response.sendRedirect();
return "11";
}
用jspSmartUpload組件來實(shí)現(xiàn),用jsp+servlet在Servlet里實(shí)現(xiàn)的代碼:
PrintWriter out = response.getWriter();
int count = 0;
// 實(shí)例化上傳控件對(duì)象
SmartUpload su = new SmartUpload();
// 初始化操作
su.initialize(config, request, response);
// 設(shè)置上傳文件最大字節(jié)數(shù)
su.setTotalMaxFileSize(100000);
//
try {
//禁止上傳指定擴(kuò)展名的文件
su.setDeniedFilesList("ext,bat,jsp");
} catch (SQLException e1) {
e1.printStackTrace();
}
try {
// 上傳文件到服務(wù)器
su.upload();
File fileup = new File(request.getRealPath("upload"));
if(!fileup.exists()){
// 創(chuàng)建目錄
fileup.mkdir();
}
// 處理多個(gè)文件的上傳
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");
如果你對(duì)這個(gè)上傳組件不了解,最好是先去查查用法。。。
如果對(duì)您有幫助,請(qǐng)記得采納為滿意答案,謝謝!祝您生活愉快!
vaela
你的意思是拷貝嗎,還是上傳到服務(wù)器什么的
import java.io.*;
/**
* 復(fù)制文件夾或文件夾
*/
public class CopyDirectory {
// 源文件夾
static String url1 = "f:/photos";
// 目標(biāo)文件夾
static String url2 = "d:/tempPhotos";
public static void main(String args[]) throws IOException {
// 創(chuàng)建目標(biāo)文件夾
(new File(url2)).mkdirs();
// 獲取源文件夾當(dāng)前下的文件或目錄
File[] file = (new File(url1)).listFiles();
for (int i = 0; i file.length; i++) {
if (file[i].isFile()) {
// 復(fù)制文件
copyFile(file[i],new File(url2+file[i].getName()));
}
if (file[i].isDirectory()) {
// 復(fù)制目錄
String sourceDir=url1+File.separator+file[i].getName();
String targetDir=url2+File.separator+file[i].getName();
copyDirectiory(sourceDir, targetDir);
}
}
}
// 復(fù)制文件
public static void copyFile(File sourceFile,File targetFile)
throws IOException{
// 新建文件輸入流并對(duì)它進(jìn)行緩沖
FileInputStream input = new FileInputStream(sourceFile);
BufferedInputStream inBuff=new BufferedInputStream(input);
// 新建文件輸出流并對(duì)它進(jìn)行緩沖
FileOutputStream output = new FileOutputStream(targetFile);
BufferedOutputStream outBuff=new BufferedOutputStream(output);
// 緩沖數(shù)組
byte[] b = new byte[1024 * 5];
int len;
while ((len =inBuff.read(b)) != -1) {
outBuff.write(b, 0, len);
}
// 刷新此緩沖的輸出流
outBuff.flush();
//關(guān)閉流
inBuff.close();
outBuff.close();
output.close();
input.close();
}
// 復(fù)制文件夾
public static void copyDirectiory(String sourceDir, String targetDir)
throws IOException {
// 新建目標(biāo)目錄
(new File(targetDir)).mkdirs();
// 獲取源文件夾當(dāng)前下的文件或目錄
File[] file = (new File(sourceDir)).listFiles();
for (int i = 0; i file.length; i++) {
if (file[i].isFile()) {
// 源文件
File sourceFile=file[i];
// 目標(biāo)文件
File targetFile=new
File(new File(targetDir).getAbsolutePath()
+File.separator+file[i].getName());
copyFile(sourceFile,targetFile);
}
if (file[i].isDirectory()) {
// 準(zhǔn)備復(fù)制的源文件夾
String dir1=sourceDir + "/" + file[i].getName();
// 準(zhǔn)備復(fù)制的目標(biāo)文件夾
String dir2=targetDir + "/"+ file[i].getName();
copyDirectiory(dir1, dir2);
}
}
}
}
你說的2種方法都是很簡(jiǎn)單的,參考網(wǎng)上的資料都不難做出,用io流做更是基礎(chǔ)中的基礎(chǔ),我說下smartupload好了,有的人是直接寫在jsp上面,感覺比較亂,我一般都是寫在action里面,打好jar包和配置后
SmartUpload mySmartUpload = new SmartUpload();
//如果是struts2.0或者webwork 則是mySmartUpload.initialize(ServletActionContext.getServletConfig(),ServletActionContext.getRequest(),ServletActionContext.getResponse());
mySmartUpload.initialize(servlet.getServletConfig(), request,response);
mySmartUpload.setTotalMaxFileSize(500000);
//如果上傳任意文件不設(shè)置mySmartUpload.setAllowedFilesList(文件后綴名)就可以了
mySmartUpload.upload();
for (int i = 0; i mySmartUpload.getFiles().getCount(); i++) {
com.jspsmart.upload.File file = mySmartUpload.getFiles().getFile(i);
if (file.isMissing()) continue;
file.saveAs(保存的地址 + file.getFileName(),
su.SAVE_PHYSICAL);
新聞標(biāo)題:java上傳圖片的源代碼,java上傳下載圖片
文章鏈接:http://chinadenli.net/article23/dsehpcs.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供用戶體驗(yàn)、網(wǎng)站設(shè)計(jì)公司、網(wǎng)站設(shè)計(jì)、關(guān)鍵詞優(yōu)化、面包屑導(dǎo)航、網(wǎng)站營(yíng)銷
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)