欧美一区二区三区老妇人-欧美做爰猛烈大尺度电-99久久夜色精品国产亚洲a-亚洲福利视频一区二区

上傳圖片的代碼JAVA,web上傳圖片代碼

java怎樣上傳圖片(寫個(gè)例子謝謝);

我有一段上傳圖片的代碼,并且可以根據(jù)實(shí)際,按月或按天等,生成存放圖片的文件夾

十余年的柯橋網(wǎng)站建設(shè)經(jīng)驗(yàn),針對(duì)設(shè)計(jì)、前端、開發(fā)、售后、文案、推廣等六對(duì)一服務(wù),響應(yīng)快,48小時(shí)及時(shí)工作處理。網(wǎng)絡(luò)營(yíng)銷推廣的優(yōu)勢(shì)是能夠根據(jù)用戶設(shè)備顯示端的尺寸不同,自動(dòng)調(diào)整柯橋建站的顯示方式,使網(wǎng)站能夠適用不同顯示終端,在瀏覽器中調(diào)整網(wǎng)站的寬度,無論在任何一種瀏覽器上瀏覽網(wǎng)站,都能展現(xiàn)優(yōu)雅布局與設(shè)計(jì),從而大程度地提升瀏覽體驗(yàn)。成都創(chuàng)新互聯(lián)從事“柯橋網(wǎng)站設(shè)計(jì)”,“柯橋網(wǎng)站推廣”以來,每個(gè)客戶項(xiàng)目都認(rèn)真落實(shí)執(zhí)行。

首先在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";

}

app怎么上傳圖片到j(luò)ava后臺(tái)?java后臺(tái)處理的具體代碼是怎么寫的?

使用一些已有的組件幫助我們實(shí)現(xiàn)這種上傳功能。常用的上傳組件:Apache的CommonsFileUploadJavaZoom的UploadBeanjspSmartUpload以下,以FileUpload為例講解1、在jsp端要注意enctype="multipart/form-data"然后只需要放置一個(gè)file控件,并執(zhí)行submit操作即可2、web端核心代碼如下:publicvoiddoPost(HttpServletRequestrequest,HttpServletResponseresponse)throwsServletException,IOException{request.setCharacterEncoding("UTF-8");DiskFileItemFactoryfactory=newDiskFileItemFactory();ServletFileUploadupload=newServletFileUpload(factory);try{Listitems=upload.parseRequest(request);Iteratoritr=items.iterator();while(itr.hasNext()){FileItemitem=(FileItem)itr.next();if(item.isFormField()){System.out.println("表單參數(shù)名:"+item.getFieldName()+",表單參數(shù)值:"+item.getString("UTF-8"));}else{if(item.getName()!=null!item.getName().equals("")){System.out.println("上傳文件的大小:"+item.getSize());System.out.println("上傳文件的類型:"+item.getContentType());System.out.println("上傳文件的名稱:"+item.getName());FiletempFile=newFile(item.getName());Filefile=newFile(sc.getRealPath("/")+savePath,tempFile.getName());item.write(file);request.setAttribute("upload.message","上傳文件成功!");}else{request.setAttribute("upload.message","沒有選擇上傳文件!");}}}}catch(FileUploadExceptione){e.printStackTrace();}catch(Exceptione){e.printStackTrace();request.setAttribute("upload.message","上傳文件失敗!");}request.getRequestDispatcher("/uploadResult.jsp").forward(request,response);}

求JAVA上傳圖片代碼

public String imagesUpload(){

log.debug("images upload");

if(files == null){

saveMessage("沒有上傳任何文件!");

return ERROR;

}

// 查看上傳臨時(shí)目錄是否存在

String sep = File.separator;

String condPath = sep + "upload" + sep +"tmp" +sep;

String uploadDir = ServletActionContext.getServletContext().getRealPath(condPath) + sep;

File dirPath = new File(uploadDir);

if (!dirPath.exists())

dirPath.mkdirs();

String extension, fileName;

int count = 0;

StringBuilder picNameSB = new StringBuilder("");

ListString picNamesSet = new ArrayListString();

try{

for(File tempFile : files){

if(count0)

picNameSB.append("##");

extension = UserUtil.getFileExtension(filesFileName[count]);

fileName = generatePictureName(uploadDir, doType + count, extension);

File newFile = new File(fileName);

UserUtil.copyFileContent(tempFile,newFile);

picNamesSet.add(newFile.getName());

picNameSB.append(newFile.getName());

count++ ;

}

getSession().setAttribute("picNamesSet",picNamesSet);

picName = picNameSB.toString();

}catch(IOException e){

e.printStackTrace();

return ERROR;

}

int x= 1;

return SUCCESS;

}

private String generatePictureName(final String dir, final String suffix, final String fileExtension){

StringBuffer name = new StringBuffer(dir);

String tmpFileName = String.valueOf(System.currentTimeMillis());

if(suffix != null)

tmpFileName += suffix;

tmpFileName += "." + fileExtension;

name.append(tmpFileName);

return name.toString();

}

java實(shí)現(xiàn)圖片上傳至服務(wù)器并顯示,如何做?希望要具體的代碼實(shí)現(xiàn)

很簡(jiǎn)單。

可以手寫IO讀寫(有點(diǎn)麻煩)。

怕麻煩的話使用FileUpload組件 在servlet里doPost嵌入一下代碼

public void doPost(HttpServletRequest request,HttpServletResponse response)

throws ServletException,IOException{

response.setContentType("text/html;charset=gb2312");

PrintWriter out=response.getWriter();

//設(shè)置保存上傳文件的目錄

String uploadDir =getServletContext().getRealPath("/up");

System.out.println(uploadDir);

if (uploadDir == null)

{

out.println("無法訪問存儲(chǔ)目錄!");

return;

}

//根據(jù)路徑創(chuàng)建一個(gè)文件

File fUploadDir = new File(uploadDir);

if(!fUploadDir.exists()){

if(!fUploadDir.mkdir())//如果UP目錄不存在 創(chuàng)建一個(gè) 不能創(chuàng)建輸出...

{

out.println("無法創(chuàng)建存儲(chǔ)目錄!");

return;

}

}

if (!DiskFileUpload.isMultipartContent(request))

{

out.println("只能處理multipart/form-data類型的數(shù)據(jù)!");

return ;

}

DiskFileUpload fu = new DiskFileUpload();

//最多上傳200M數(shù)據(jù)

fu.setSizeMax(1024 * 1024 * 200);

//超過1M的字段數(shù)據(jù)采用臨時(shí)文件緩存

fu.setSizeThreshold(1024 * 1024);

//采用默認(rèn)的臨時(shí)文件存儲(chǔ)位置

//fu.setRepositoryPath(...);

//設(shè)置上傳的普通字段的名稱和文件字段的文件名所采用的字符集編碼

fu.setHeaderEncoding("gb2312");

//得到所有表單字段對(duì)象的集合

List fileItems = null;

try

{

fileItems = fu.parseRequest(request);//解析request對(duì)象中上傳的文件

}

catch (FileUploadException e)

{

out.println("解析數(shù)據(jù)時(shí)出現(xiàn)如下問題:");

e.printStackTrace(out);

return;

}

//處理每個(gè)表單字段

Iterator i = fileItems.iterator();

while (i.hasNext())

{

FileItem fi = (FileItem) i.next();

if (fi.isFormField()){

String content = fi.getString("GB2312");

String fieldName = fi.getFieldName();

request.setAttribute(fieldName,content);

}else{

try

{

String pathSrc = fi.getName();

if(pathSrc.trim().equals("")){

continue;

}

int start = pathSrc.lastIndexOf('\\');

String fileName = pathSrc.substring(start + 1);

File pathDest = new File(uploadDir, fileName);

fi.write(pathDest);

String fieldName = fi.getFieldName();

request.setAttribute(fieldName, fileName);

}catch (Exception e){

out.println("存儲(chǔ)文件時(shí)出現(xiàn)如下問題:");

e.printStackTrace(out);

return;

}

finally //總是立即刪除保存表單字段內(nèi)容的臨時(shí)文件

{

fi.delete();

}

}

}

注意 JSP頁面的form要加enctype="multipart/form-data" 屬性, 提交的時(shí)候要向服務(wù)器說明一下 此頁面包含文件。

如果 還是麻煩,干脆使用Struts 的上傳組件 他對(duì)FileUpload又做了封裝,使用起來更傻瓜化,很容易掌握。

-----------------------------

以上回答,如有不明白可以聯(lián)系我。

分享文章:上傳圖片的代碼JAVA,web上傳圖片代碼
當(dāng)前網(wǎng)址:http://chinadenli.net/article8/heesip.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供做網(wǎng)站、網(wǎng)站排名外貿(mào)建站、品牌網(wǎng)站建設(shè)電子商務(wù)、網(wǎ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)

微信小程序開發(fā)