1. 程式人生 > >用jsp實現簡單的圖片上傳功能

用jsp實現簡單的圖片上傳功能

public voiddoGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
   String contentType=request.getContentType();
   String servername=request.getServerName();
   String realpath=request.getRealPath(servername);
   System.out.println(contentType);

   InputStream in=null;
   OutputStream out=null;
   if(contentType.indexOf("multipart/form-data")>=0){
    in=request.getInputStream();
    int formlength=request.getContentLength();
    byte[] formcontent=new byte[formlength];
    int totalread=0;
    int nowread=0;
    while(totalread<formlength){
     nowread=in.read(formcontent,totalread, formlength);
     totalread+=nowread;
    }
    String strcontent=new String(formcontent);
    System.out.println(strcontent);
    int typestart=strcontent.indexOf("Content-Type:")+14;
    int typeend=strcontent.indexOf("\n", typestart)-1;
    String formType=strcontent.substring(typestart, typeend);
   if(formType.equals("image/jpeg")||formType.equals("image/gif")||formType.equals("image/pjepg")){
     intfilenamestart=strcontent.indexOf("filename=\"")+10;
     intfilenameend=strcontent.indexOf("\n",filenamestart)-2;
     String filename=strcontent.substring(filenamestart,filenameend);
    filename=filename.substring(filename.lastIndexOf("."));
     String newfilename=""+(newDate()).getDate()+(new Date()).getHours()+(new Date()).getMinutes()+(newDate()).getSeconds();
     newfilename=newfilename+filename;
     realpath=realpath+"";
     newfilename=realpath+newfilename;
     int filestart=strcontent.indexOf("\n",typestart)+1;
    filestart=strcontent.indexOf("\n",filestart)+1;
     intintboundary=contentType.indexOf("boundary=")+10;
     String strboundary=contentType.substring(intboundary);
     int fileend=strcontent.indexOf(strboundary,filestart)-4;
     StringsaveFile=strcontent.substring(filestart,fileend);
     intcontentstart=strcontent.substring(0,filestart).getBytes().length;
     int contentend=strcontent.substring(0,fileend).getBytes().length;
     System.out.println(saveFile);
     File myfile=new File(realpath);
     if(!myfile.exists()){
      myfile.mkdirs();
     }
     out=new FileOutputStream(newfilename);
     out.write(formcontent, contentstart,contentend-contentstart);
     response.sendRedirect("show.jsp");
    }else{
     response.sendRedirect("error.jsp");
    }
   }
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
   doGet(request,response);
}

相關推薦

TP5.0整合webuploader實現圖片功能

//**其實這一段js就是upload.js粘過來的,改成自己的路徑** var imgurl = new Array();//這個是自己新增的,用於把多張圖片的路徑放到這個jQuery陣列中然後賦值到表單提交 (function( $ ){ // 當domReady的時候開始

jsp實現簡單圖片功能

public voiddoGet(HttpServletRequest request, HttpServletResponse response)     throws ServletException, IOException {    String contentType=request.getCont

Vue2.0 引用 exif.js 實現調攝像頭進行拍照功能以及圖片功能

[0 complete num else imp ada blob can isf vue組件代碼 <template> <div> <div style="padding:20px;"> <div cla

PHP圖片功能實現

php file 圖片上傳 上傳圖片功能:獲取當前文件夾位置,上傳到當前文件夾下為了上傳後文件名重復導致覆蓋,上傳後更改名字為:當前時間+原文件名只允許gif、jpeg、bmg、jpg格式的文件上傳<form name="frm1" enctype="multipart/form-data"

elementUI+koa實現圖片功能

elementUI中的上傳元件為: 大家可以先在自己的專案中執行一下餓了麼上傳元件 <el-upload action="https://jsonplaceholder.typicode.com/posts/" list-type="picture-card" :o

CKEditor4.7之使用Java實現圖片功能

1.官網下載CKEditor,基礎包,標準包,全包選擇其一。地址:https://ckeditor.com/ckeditor-4/download/ 2.下載檔案後將其解壓至WebRoot資料夾下,解壓前可以新建一個包檔案FckEditor 3.開啟ckeditor\plugin

使用 MultipartFile 結合 (formData物件、Blob物件) 實現圖片功能簡介

圖片上傳功能的具體實現(當然檔案也一樣): MultipartFile ,這個類可以完全接收到前臺傳過來的圖片資料。 MultipartFile 通過 MultipartFile .transferTo( new File()), 僅需要這步驟,就可以把圖片存到伺

【移動端實現】相機喚起及圖片功能(包括微信)

前言 目前有個需求是圖片拍照上傳的功能,但是我就是死活掉不起相機。。。總是開啟的是檔案目錄OR上傳圖片。。。。而不是相機。。。 所以對比了下 程式碼: 參考了百度移動端實現方式。。。程式碼很簡單,對就這一句! <input type="file" acce

vue簡單製作圖片功能

樣式 .iul-img{ width:100%; height:100%; } .idcard-ul{ display:flex; } .iu-item,.iu-img{ width:250px; height:250px; border:1px solid #

Android實現拍照相簿圖片功能

更改頭像功能不像修改資訊一樣直接提交引數就可以,需要上傳圖片檔案 我就直接貼程式碼了首先給出佈局檔案 <ImageView android:id="@+id/iv" android:layout_width="50d

微信小程式實現圖片功能

前端: 微信開發者工具 後端:.Net 伺服器:阿里雲 這裡介紹微信小程式如何實現上傳圖片到自己的伺服器上 前端程式碼 data: { productInfo: {} }, //上傳圖片 uploadImage: function () { var that

java多圖片功能實現

開發環境:jdk1.7,MyEclipse10 框架用的是spring。用到了maven工具(maven的包百度下就可以)。 四步完成,全部複製改引數就可以 第一步:先在Spring中對圖片進行限制 <!-- SpringMVC上傳檔案時,

React Native 使用react-native-image-picker庫實現圖片功能

react-native-image-picker作為一個整合相機和相簿的功能的第三方庫,因為其使用相對簡單受到前端開發人員的喜愛。 react-native-image-picker使用 1, 首先,安裝下該外掛。 npm install rea

Springboot圖片功能實現 檔案

1、配置上傳路徑 web: upload-path: E:/img/ #自定義檔案上傳路徑 multipart: maxRequestSize: 2Mb #設定所有檔案最大記憶體 maxF

vue + multer+mongodb實現文字加圖片功能

前端程式碼: <template> <div> <form name="fileinfo" id="myForm"> 標題: <input type="text

《React-Native系列》26、 ReactNative實現圖片功能

在檢視ReactNative的官方文件的時候,你會發現其實Fackbook是沒有提供圖片上傳功能的。如果我們的專案裡需要使用圖片上傳(用JS 實現圖片上傳),那我們有沒有什麼辦法呢?解決方案:利用FormData物件,你可以使用一系列的鍵值對來模擬一個完整的表單,然後使用XM

Vue2.0實現呼叫攝像頭進行拍照功能以及圖片功能引用exif.js

(function() { var debug = false; var root = this; var EXIF = function(obj) { if (obj instanceof EXIF) return obj; if (!(this instanceof EXIF

java模擬表單檔案,java通過模擬post方式提交表單實現圖片功能例項

package com.zdz.httpclient;import java.io.BufferedReader;import java.io.DataInputStream;import java.io.DataOutputStream;import java.io.File;import java.io.

前端實現圖片檢視功能(帶UI實現

由於業務需求,需要實現多圖片的上傳,並且能夠實時檢視使用者上傳的圖片列表。因此從網上找了一個不錯的外掛,並和bootstrap UI進行的相容。基本能夠滿足需求。下面就是我的實現過程(PS:本人前端了解不太深,所以基本是實現了功能… UI沒有太多考慮…)

js圖片功能實現 FileReader()

想要實現的功能是,點選一個上傳的按鈕,然後選中上傳圖片,在頁面上可以看到上傳的圖片 一開始是 點選這個+之後,上傳的圖片就出現在+上,將其覆蓋 實現的思路如下: html頁面上的要有input type=file 來上傳檔案,由於上傳按鈕太醜了,所有將其隱藏起