由於要做上傳 就找了個例子 留著參考
上傳:需要smartupload組件
import com.jspsmart.upload.*;<br /> import javax.servlet.*;<br /> import javax.servlet.http.*;<br /> import java.io.*;<br /> import java.util.*;<br /> /**<br /> * 上傳公用類,對SmartUpload進行了封裝<br /> * @author askr<br /> * @version 1.0<br /> */<br /> public class MyUpload<br /> {<br /> /**<br /> *儲存上傳的檔案<br /> *<br /> * @param request 頁面請求對象<br /> * @param response 頁面響應對象<br /> * @param config 頁面設定物件<br /> * @param mainName 賦予的主檔案名<br /> * @param savePhyPath 檔案儲存的檔案夾<br /> * @param maxSizePool 上傳時允許的最大容量,單位:KB<br /> * @param maxSizeAllowed 檔案上傳時允許的最大大小,單位:KB<br /> * @param formatListAllowed 允許的檔案格式,以逗號分隔的字串序列<br /> * @return 如果沒有上傳,返回Null 字元串,否則,是儲存後的檔案名稱<br /> */<br /> public static String SaveUpload(HttpServletRequest request,<br /> HttpServletResponse response,ServletConfig config,<br /> String mainName,String savePhyPath,int maxSizePool,int maxSizeAllowed,<br /> String formatListAllowed)<br /> {<br /> SmartUpload upl = new SmartUpload();<br /> com.jspsmart.upload.File f=null;<br /> try {<br /> upl.init(config); //config是隱含內建對象<br /> upl.service(request, response);<br /> // upl.setAllowedFilesList(formatListAllowed);<br /> upl.setTotalMaxFileSize(maxSizePool * 1024); //位元組<br /> upl.upload();<br /> f = upl.getFiles().getFile(0);<br /> String name = f.getFileName();<br /> if (f.getSize() == 0) {<br /> return "";<br /> }<br /> if (f.getSize() > maxSizeAllowed * 1024)<br /> throw new RuntimeException("最大大小為" +<br /> maxSizeAllowed + "KB");<br /> String ext=f.getFileExt();<br /> StringTokenizer st=new StringTokenizer(formatListAllowed,",");<br /> boolean formatFlag=false;<br /> while (st.hasMoreTokens())<br /> {<br /> String element=st.nextToken();<br /> if (element.equalsIgnoreCase(ext))<br /> {<br /> formatFlag = true;<br /> break;<br /> }<br /> }<br /> if (formatFlag==false) throw new RuntimeException("檔案格式不對,必須是"+<br /> formatListAllowed+"中的一種");<br /> String path = savePhyPath + "//" + mainName + "." +<br /> f.getFileExt();<br /> f.saveAs(path,com.jspsmart.upload.File.SAVEAS_PHYSICAL);<br /> }<br /> catch (RuntimeException ex) {<br /> throw ex;<br /> }<br /> catch(IOException ex)<br /> {<br /> ex.printStackTrace();<br /> }<br /> catch (SmartUploadException ex)<br /> {<br /> ex.printStackTrace();<br /> }<br /> catch (ServletException ex) {<br /> ex.printStackTrace();<br /> }<br /> return mainName + "." +f.getFileExt();<br /> }<br /> } </p><p> 產生縮圖:<br /> import com.sun.image.codec.jpeg.*;<br /> import java.awt.*;<br /> import java.awt.image.BufferedImage;<br /> import java.io.*;<br /> import javax.imageio.*;<br /> import com.sun.image.codec.jpeg.JPEGImageEncoder;<br /> public class CompressImage<br /> {<br /> /**<br /> *<br /> * @param oldPath:需要壓縮的檔案完整實體路徑,包括名字<br /> * @param newDir:新檔案的儲存路徑,不含名字<br /> * @param newMainName 新檔案的主檔案名<br /> * @param maxBorder 新檔案的長度和寬度裡面最長的邊的長度,按比例縮小另外的一個邊,比如要產生<br /> * 的圖形最長的邊是64像素,如果是長度比寬度大,那麼,產生的長度是64,而寬度就是原始的寬度的等比例縮小的尺寸。<br /> */<br /> public static void Generate(String oldPath,String newDir,String newMainName,int maxBorder)<br /> {<br /> File souFile=new File(oldPath);<br /> // String a=souFile.getAbsolutePath();<br /> // int a1=1;<br /> try {<br /> String newPath;<br /> if (newDir.equals(""))<br /> newPath = newMainName + ".jpg";<br /> else<br /> newPath =newDir + "//" + newMainName + ".jpg";<br /> FileOutputStream desFile = new FileOutputStream(new<br /> File(newPath));<br /> BufferedImage souImage = ImageIO.read(souFile);<br /> int oldWidth = souImage.getWidth();<br /> int oldHeight = souImage.getHeight();<br /> int newWidth, newHeight;<br /> if (oldWidth > oldHeight) {<br /> newWidth = maxBorder;<br /> newHeight = (int) (newWidth * oldHeight /<br /> oldWidth);<br /> }<br /> else {<br /> newHeight = maxBorder;<br /> newWidth = (int) (oldWidth * newHeight /<br /> oldHeight);<br /> }<br /> BufferedImage desImage = new BufferedImage(<br /> newWidth, newHeight,1);<br /> desImage.getGraphics().drawImage(souImage,0,0,newWidth,newHeight,<br /> null);<br /> JPEGImageEncoder jpegEncode = JPEGCodec.createJPEGEncoder(desFile);<br /> jpegEncode.encode(desImage);<br /> desFile.close();<br /> }<br /> catch (ImageFormatException ex) {<br /> ex.printStackTrace();<br /> }<br /> catch (IOException ex) {<br /> ex.printStackTrace();<br /> }<br /> catch (Exception e)<br /> {<br /> e.printStackTrace();<br /> }<br /> }<br /> }</p><p>///////////////////////////////////////////////////////////////////////servlet方法如下:</p><p> javabean:<br /> mport java.io.*; </p><p> public class dbo {<br /> public dbo() {<br /> } </p><p> public InputStream getBytes() { </p><p> Connection con = null;<br /> PreparedStatement pst = null;<br /> ResultSet rs = null;<br /> InputStream stream = null;<br /> File f = null;<br /> byte context[] = new byte[100000];<br /> try {<br /> Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");<br /> con = DriverManager.getConnection("jdbc:odbc:test");<br /> f = new File("C://aa.jpg");<br /> stream = new FileInputStream(f);<br /> //pst = con.prepareStatement("INSERT INTO MyTable (COL10) VALUES(?)");<br /> //pst.setBinaryStream(1,stream,(int)f.length());<br /> pst = con.prepareStatement("SELECT COL10 FROM MyTable");<br /> rs = pst.executeQuery();<br /> if (rs.next()) {<br /> stream = rs.getBinaryStream(1);<br /> }<br /> return stream;<br /> }<br /> catch (Exception e) {<br /> System.out.println(e);<br /> return stream;<br /> }<br /> finally {<br /> try {<br /> /*<br /> if (rs != null) {<br /> rs.close();<br /> }<br /> if (pst != null) {<br /> pst.close();<br /> }<br />if (con != null) {<br /> pst.close();<br /> }<br /> */<br /> }<br /> catch (Exception e) { </p><p> }<br /> } </p><p> } </p><p> public static void main(String args[]) { </p><p> }<br /> } </p><p> /*<br /> 寫入資料庫<br /> Connection con = null;<br /> PreparedStatement pst = null;<br /> ResultSet rs = null;<br /> InputStream stream = null;<br /> File f=null;<br /> try {<br /> Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");<br /> con = DriverManager.getConnection("jdbc:odbc:test");<br /> f = new File("C://aa.jpg");<br /> stream = new FileInputStream(f);<br /> pst = con.prepareStatement("INSERT INTO MyTable (COL10) VALUES(?)");<br /> pst.setBinaryStream(1,stream,(int)f.length());<br /> pst.execute();<br /> }<br /> catch (Exception e) {<br /> System.out.println(e);<br /> }<br /> finally {<br /> try {<br /> if (rs != null) {<br /> rs.close();<br /> }<br /> if (pst != null) {<br /> pst.close();<br /> }<br /> if (con != null) {<br /> pst.close();<br /> }<br /> }<br /> catch (Exception e) { </p><p> }<br /> }<br /> */ </p><p> jsp and servlet<br /> jsp:<br /> <%@ page contentType="text/html; charset=GBK" %><br /> <html><br /> <head><br /> <title><br /> jsp1<br /> </title><br /> </head><br /> <body bgcolor="#ffffff"><br /> dfdfd<br /> <img src="servlet1?id=1" mce_src="servlet1?id=1"/><br /> dfdef<br /> </body><br /> </html> </p><p> servlet: </p><p> package index; </p><p> import javax.servlet.*;<br /> import javax.servlet.http.*;<br /> import java.io.*;<br /> //import java.util.*;<br /> import index.dbo; </p><p> public class Servlet1<br /> extends HttpServlet {<br /> private static final String CONTENT_TYPE = "text/html; charset=GBK"; </p><p> //Initialize global variables<br /> public void init() throws ServletException {<br /> } </p><p> //Process the HTTP Get request<br /> public void doGet(HttpServletRequest request, HttpServletResponse response) throws<br /> ServletException, IOException {<br /> //response.setContentType("image/gif;");<br /> dbo d = new dbo();<br /> java.io.InputStream pic = null;<br /> java.io.OutputStream pw = null;<br /> try{<br /> //request.getParameter("id");<br /> response.setContentType("image/gif;name=doskt.jpg");<br /> pic=d.getBytes();<br /> pw=response.getOutputStream();<br /> byte[] b = new byte[4096];<br /> int bt = pic.read(b);<br /> while (bt != -1) {<br /> pw.write(b, 0, bt);<br /> bt = pic.read(b);<br /> }<br /> }<br /> catch(Exception e){<br /> System.out.println("doGet:" + e);<br /> }<br /> finally{<br /> if(pic!=null){<br /> pic.close();<br /> }<br /> } </p><p> /*out.println("<html>");<br /> out.println("<head><title>Servlet1</title></head>");<br /> out.println("<body bgcolor=/"#ffffff/">");<br /> out.println("<p>The servlet has received a GET. This is the reply.</p>");<br /> out.println("</body></html>");*/<br /> } </p><p> //Clean up resources<br /> public void destroy() {<br /> }<br /> }<br />文章出處:DIY部落(http://www.diybl.com/course/4_webprogram/jsp/jsp_js/2008430/112053_4.html)</p><p>