上傳圖片
------
使用apache的上傳附件插件:commons-fileupload.jar
commons-io.jar
A:創建頁面:upload.jsp
B:編寫servlet:PicUploadServlet.java
c:配置web.xml
jsp
-----------
<!-- 上傳附件的表單(二進制數據)注意兩點:
1、post請求,
2、enctype="multipart/form-data" -->
<form name="myForm" action="${pageContext.request.contextPath }/picUpload"
method="post" enctype="multipart/form-data">
username:<input type="text" name="username"><br/>
file:<input type="file" name="file" />
<input type="submit" value="上傳">
</form>
servlet
---------
public class PicUploadServlet extends HttpServlet
{
private File tempPath = null;
private File uploadPath = null;
//初始化文件上傳的目錄
//1、存儲圖片的臨時路徑
//2、存放(上傳)文件的真實路徑
@Override
public void init()
throws ServletException
{
//獲取臨時交換目錄
tempPath = new File(this.getServletContext().getRealPath("temp"));
System.out.println(this.getServletContext().getRealPath("temp"));
if(!tempPath.exists())
{
tempPath.mkdir();
}
//獲取上傳文件的真實路徑
uploadPath = new File(this.getServletContext().getRealPath("upload/images"));
if(!uploadPath.exists())
{
//創建多級目錄
uploadPath.mkdirs();
}
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException
{
DiskFileItemFactory factory = new DiskFileItemFactory();
//設置文件上傳的大小塊4k
factory.setSizeThreshold(4096);
factory.setRepository(tempPath);
//創建上傳組件
ServletFileUpload upload = new ServletFileUpload(factory);
//設置文件上傳大小 4M
upload.setSizeMax(4096000);
//得到表單元素列表
try
{
List fileItems = upload.parseRequest(req);
//遍歷該集合
Iterator fileItem = fileItems.iterator();
String username = "";
while(fileItem.hasNext())
{
FileItem item = (FileItem)fileItem.next();
//判斷該元素是否爲普通表單元素
if(item.isFormField())
{
if("username".equals(item.getFieldName()))
{
username = item.getString("utf-8");
System.out.println(username);
}
}
//附件上傳元素
else
{
String fileName = item.getName();
//獲取後綴名
String ext = fileName.substring(fileName.lastIndexOf(".")+1, fileName.length());
//得到一個不會重複的文件名
Calendar c = Calendar.getInstance();
fileName = String.valueOf(c.getTimeInMillis());
//fileName = c.getTimeInMillis()+"";
//拼接一個帶不會重複的文件名的全路徑
String fullFilePath = uploadPath+"/"+fileName+"."+ext;
//將緩存中的二進制流的附件,寫入該路徑(附件存放在磁盤下的具體路徑)
File f = new File(fullFilePath);
item.write(f);
//將上面的原圖同時生成一張等比例縮放的縮略圖
//創建一個縮略圖的全路徑
String newurl = uploadPath+"/"+fileName+"_min."+ext;
//構建image對象
Image src = ImageIO.read(f);
//設置目標大小
float tagSize = 200;
int oldWidth = src.getWidth(null);
int oldHeight = src.getHeight(null);
//定義新的高度和寬度
int newWidth=0;
int newHeight=0;
//聲明臨時大小
float tempDouble=0;
//橫放
if(oldWidth > oldHeight)
{
tempDouble = oldWidth/tagSize;
}
else
{
tempDouble = oldHeight/tagSize;
}
//得到新的高度和寬度
newWidth = Math.round(oldWidth/tempDouble);
newHeight = Math.round(oldHeight/tempDouble);
//創建新圖對象
BufferedImage tag = new BufferedImage(newWidth, newHeight, BufferedImage.TYPE_INT_RGB);
tag.getGraphics().drawImage(src, 0, 0,newWidth,newHeight,null);
FileOutputStream newImage = new FileOutputStream(newurl);
//創建編碼對象
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(newImage);
//JPEG編碼
encoder.encode(tag);
//關閉流
newImage.close();
}
}
req.getRequestDispatcher("success.jsp").forward(req, resp);
}
catch (FileUploadException e)
{
e.printStackTrace();
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
上傳圖片功能源碼
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.