上傳圖片

別的不說,先貼代碼。

1.FileUtil.java

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;


/**
 * 文件處理工具
 * @author niuhailiang
 *
 */
public class FileUtil {
	/**
	 * 複製
	 * @param src
	 * @param dest
	 * @return
	 */
	public static boolean copy(File src,File dest){
		BufferedInputStream bis=null;
		BufferedOutputStream bos=null;
		try {
			bis=new BufferedInputStream(new FileInputStream(src));
			bos=new BufferedOutputStream(new FileOutputStream(dest));
			byte[]bts=new byte[1024];
			int length=-1;
			while((length=bis.read(bts))!=-1){
				bos.write(bts, 0, length);
				
			}
			return true;
			
		} catch (Exception e) {
			e.printStackTrace();
			return false;
			
		}finally{
			if(bis!=null){
				try {
					bis.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if(bos!=null){
				try {
					bos.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		
		
	}
}

2.部分上傳代碼

public String add() {
		
		//先上傳
		System.out.println(".........."+goods.getGoods_name());
		   src=new String[some.size()];
			for(int i=0;i<some.size();i++){
				
				//將文件放到項目部署的路徑下的upload文件夾下
			 path="/upload/"+someFileName.get(i);
				//根據相對部署的路徑計算完整地路徑
				path=ServletActionContext.getServletContext().getRealPath(path);
				System.out.println(path);
				//將臨時的文件複製到上述路徑下
				FileUtil.copy(some.get(i),new File(path));
			
				src[i]=someFileName.get(i);
			}
		
		
		System.out.println("進入添加方法");
		goods.setCategory(categoryServcie.findById(category_id));
		goods.setImages(src);
		goodsService.add(goods);
		ActionContext.getContext().put("url", "goods_list");
			return "redirect";
		
		
	}

3.頁面操作(部分)

<form action="goods_add.action" method="post" name="form" id="form" enctype="multipart/form-data" >
			<div class="MainDiv">
				<table width="99%" border="0" cellpadding="0" cellspacing="0"
					class="CContent">
					<tr>
						<th class="tablestyle_title">
							商品基本信息錄入
						</th>
					</tr>
					<tr>
						<td class="CPanel">

							<table border="0" cellpadding="0" cellspacing="0"
								style="width: 100%">

								<tr>
									<td width="100%">
										<fieldset style="height: 100%;">
											<legend>
												商品信息
											</legend>
											<table border="0" cellpadding="2" cellspacing="1"
												style="width: 100%">

												<tr>
													<td align="center">
														商品名:
													</td>
													<td >
														<input name="goods_name" />
													</td>
												</tr>
												<tr>
													<td align="center">
														商品類別:
													</td>
													<td>
														<s:select name="category_id" list="#categoryList" listKey="id"
															listValue="name" 
															id="vip">
														</s:select>

													</td>
												</tr>
												<tr>
													<td align="center">
														價格:
													</td>
													<td>
														<input name="price" />
													</td>
													
													
													
												</tr>
												<tr>
													<td align="center">
														會員價格:
													</td>
													<td>
														<input name="vip_price"   />
													</td>

												</tr>
												
												<tr>
													<td align="center">
														圖片:
													</td>
													<td>
														<input name="some" type="file"  />
													</td>
													<td>
														<input name="some" type="file"  />
													</td>
													<td>
														<input name="some" type="file"  />
													</td>
													<td>
														<input name="some" type="file"  />
													</td>

												</tr>
												<tr>
													<td align="center">
														描述:
													</td>
													<td>
														<input name="description" />

													</td>
												</tr>
												
											</table>
											<br />
										</fieldset>
									</td>

								</tr>
							</table>
						</td>
					</tr>
					<tr>
						<td colspan="2" align="center" height="50px">
							<input type="button" name="Submit" value="保存" 
								onclick="link();" />

							<input type="button" name="Submit2" value="返回" 
								onclick="window.history.go(-1);" />
						</td>
					</tr>
				</table>


				</td>
				</tr>



				</table>

			</div>
		</form>

4,顯示圖片(上傳的圖片到服務器,但是數據庫中保存了上傳的路徑),輪播顯示

<div id="wrapper">
			<div id="carousel">
			
				<ul>
				<s:iterator value="p_w_picpaths" status="status">
				
				
				<li><img src="${pageContext.request.contextPath}/upload/<s:property value="p_w_picpaths[#status.index]"/>" alt="" /><span>Image</span></li>
				
				
				</s:iterator>
									
				</ul>
				<div class="clearfix"></div>
				<a id="prev" class="prev" href="#">&lt;</a>
				<a id="next" class="next" href="#">&gt;</a>
				<div id="pager" class="pager"></div>
			</div>
		</div>



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章