天天看点

java怎样图片上传_java图片上传

package com;

import java.io.*;

import javax.servlet.*;

import javax.servlet.http.*;

import javax.servlet.jsp.*;

import com.jspsmart.upload.*;

public class uploadfiles extends HttpServlet

{

public void doGet(HttpServletRequest request,HttpServletResponse response)

throws ServletException,IOException

{

//浣跨?ㄤ?涓?涓?绗?涓??圭??缁?浠讹?瀛??惧??eb-inf/lib涓?

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

//?变?SmartUpload????濮????规???瑕?pageContext,??浠ユ??浠???ervlet涓?寰??颁?

//涓轰?寰???ageConext瑕?棣???寰???spFactory??瀹?渚?

//??杩?JspFactory??瀹?渚???getPageContext?规?寰???ageConext??瀹?渚?

JspFactory jf = null;

//寰???spFactory??瀹?渚?

jf=JspFactory.getDefaultFactory();

PageContext pageContext=jf.getPageContext(this,request,response,null,true,8192,true);

try

{

//瀹?渚???SmartUpload

SmartUpload mySmartUpload=new SmartUpload();

//??濮???SmartUpload??瀹?渚?锛???瑕?PageContext??瀹?渚?

mySmartUpload.initialize(pageContext);

//璁惧????澶т?浼???瀛????帮??跺????浠ヤ?杩?琛?璁惧??锛?琛ㄧず涓?浼?????浠舵病??澶у?????

//mySmartUpload.setTotalMaxFileSize(10000000);

mySmartUpload.upload();

//涓??㈡??????浠朵?浼?

//涓?浼?????浠朵互com.jspsmart.upload.File 浠h〃锛?濡?????浠跺??绉伴??澶?锛???杩?琛?瑕???

com.jspsmart.upload.File file=mySmartUpload.getFiles().getFile(0);

String upLoadFileName=file.getFileName();

//璋???om.jspsmart.upload.File瀹?渚???saveas???规?淇?瀛???浠讹?姝ゆ?剁????浠跺???虫??

//淇?瀛??版???″?ㄤ?????浠跺??

file.saveAs("/upload/"+upLoadFileName);

Request req =

Text t = .....;

t.setUpload(upLoadFileName);

t.set.....(req);

}

catch(SmartUploadException e)

{

System.out.println(e.getMessage());

}

}

protected void doPost(HttpServletRequest request, HttpServletResponse response)

throws ServletException, java.io.IOException

{

doGet(request,response);

}

}