導航:首頁 > 編程語言 > java圖片上傳

java圖片上傳

發布時間:2022-01-27 07:38:57

java圖片上傳

public class UploadBean {
private String saveDir = "."; // 要保存文件的路徑
private String charset = ""; // 字元集
private ArrayList<Object> tmpFileName = new ArrayList<Object>(); // 臨時存放文件名的數據結構
private Hashtable<String,String> parameter = new Hashtable<String,String>(); // 存放參數名和值的數據結構
private HttpServletRequest request; // 用於傳入請求對象的實例
private String boundary = ""; // 內存數據的分隔符
private int len = 0; // 每次從內在中實際讀到的位元組長度
private String queryString;
private int count; // 上載的文件總數
private String[] fileName; // 上載的文件名數組
private long maxFileSize = 1024*1024 * 1024 * 1; // 最大文件上載位元組;
private String tagFileName = ""; public final void init(HttpServletRequest request) throws ServletException {
this.request = request;
boundary = request.getContentType().substring(30); // 得到內存中數據分界符
queryString = request.getQueryString();
} public String getParameter(String s) { // 用於得到指定欄位的參數值,重寫request.getParameter(String
// s)
if (parameter.isEmpty()) {
return null;
}
return (String) parameter.get(s);
} public String[] getParameterValues(String s) { // 用於得到指定同名欄位的參數數組,重寫request.getParameterValues(String
// s)
ArrayList<String> al = new ArrayList<String>();
if (parameter.isEmpty()) {
return null;
}
Enumeration<String> e = parameter.keys();
while (e.hasMoreElements()) {
String key = (String) e.nextElement();
if (-1 != key.indexOf(s + "||||||||||") || key.equals(s)) {
al.add(parameter.get(key));
}
}
if (al.size() == 0) {
return null;
}
String[] value = new String[al.size()];
for (int i = 0; i < value.length; i++) {
value[i] = (String) al.get(i);
}
return value;
} public String getQueryString() {
return queryString;
} public int getCount() {
return count;
} public String[] getFileName() {
return fileName;
} public void setMaxFileSize(long size) {
maxFileSize = size;
} public void setTagFileName(String filename) {
tagFileName = filename;
} public void setSaveDir(String saveDir) { // 設置上載文件要保存的路徑
this.saveDir = saveDir;
File testdir = new File(saveDir); // 為了保證目錄存在,如果沒有則新建該目錄
if (!testdir.exists()) {
testdir.mkdirs();
}
} public void setCharset(String charset) { // 設置字元集
this.charset = charset;
} public boolean uploadFile() throws ServletException, IOException { // 用戶調用的上載方法
setCharset(request.getCharacterEncoding());
return uploadFile(request.getInputStream());
} private boolean uploadFile(ServletInputStream servletinputstream) throws // 取得央存數據的主方法
ServletException, IOException {
String line = null;
byte[] buffer = new byte[4096];
while ((line = readLine(buffer, servletinputstream, charset)) != null) {
if (line.startsWith("Content-Disposition: form-data;")) {
int i = line.indexOf("filename=");
if (i >= 0) { // 如果一段分界符內的描述中有filename=,說明是文件的編碼內容
String fName = getFileName(line);
if (fName.equals("")) {
continue;
}
if (count == 0 && tagFileName.length() != 0) {
String ext = fName.substring((fName.lastIndexOf(".") + 1));
fName = tagFileName + "." + ext;
}
tmpFileName.add(fName);
count++;
while ((line = readLine(buffer, servletinputstream, charset)) != null) {
if (line.length() <= 2) {
break;
}
}
File f = new File(saveDir, fName);
FileOutputStream dos = new FileOutputStream(f);
long size = 0l;
while ((line = readLine(buffer, servletinputstream, null)) != null) {
if (line.indexOf(boundary) != -1) {
break;
}
size += len;
if (size > maxFileSize) {
throw new IOException("文件超過" + maxFileSize + "位元組!");
}
dos.write(buffer, 0, len);
}
dos.close();
} else { // 否則是欄位編碼的內容
String key = getKey(line);
String value = "";
while ((line = readLine(buffer, servletinputstream, charset)) != null) {
if (line.length() <= 2) {
break;
}
}
while ((line = readLine(buffer, servletinputstream, charset)) != null) { if (line.indexOf(boundary) != -1) {
break;
}
value += line;
}
put(key, value.trim(), parameter);
}
}
}
if (queryString != null) {
String[] each = split(queryString, "&");
for (int k = 0; k < each.length; k++) {
String[] nv = split(each[k], "=");
if (nv.length == 2) {
put(nv[0], nv[1], parameter);
}
}
}
fileName = new String[tmpFileName.size()];
for (int k = 0; k < fileName.length; k++) {
fileName[k] = (String) tmpFileName.get(k); // 把ArrayList中臨時文件名倒入數據中供用戶調用
}
if (fileName.length == 0) {
return false; // 如果fileName數據為空說明沒有上載任何文件
}
return true;
} private void put(String key, String value, Hashtable<String,String> ht) {
if (!ht.containsKey(key)) {
ht.put(key, value);
} else { // 如果已經有了同名的KEY,就要把當前的key更名,同時要注意不能構成和KEY同名
try {
Thread.sleep(1); // 為了不在同一ms中產生兩個相同的key
} catch (Exception e) {
}
key += "||||||||||" + System.currentTimeMillis();
ht.put(key, value);
}
} /*
* 調用ServletInputstream.readLine(byte[] b,int
* offset,length)方法,該方法是從ServletInputstream流中讀一行
* 到指定的byte數組,為了保證能夠容納一行,該byte[]b不應該小於4096,重寫的readLine中,調用了一個成員變數len為
* 實際讀到的位元組數(有的行不滿4096),則在文件內容寫入時應該從byte數組中寫入這個len長度的位元組而不是整個byte[]
* 的長度,但重寫的這個方法返回的是String以便分析實際內容,不能返回len,所以把len設為成員變數,在每次讀操作時 把實際長度賦給它.
* 也就是說在處理到文件的內容時數據既要以String形式返回以便分析開始和結束標記,又要同時以byte[]的形式寫到文件 輸出流中.
*/
private String readLine(byte[] Linebyte,
ServletInputStream servletinputstream, String charset) {
try {
len = servletinputstream.readLine(Linebyte, 0, Linebyte.length);
if (len == -1) {
return null;
}
if (charset == null) {
return new String(Linebyte, 0, len);
} else {
return new String(Linebyte, 0, len, charset);
} } catch (Exception _ex) {
return null;
} } private String getFileName(String line) { // 從描述字元串中分離出文件名
if (line == null) {
return "";
}
int i = line.indexOf("filename=");
line = line.substring(i + 9).trim();
i = line.lastIndexOf("");
if (i < 0 || i >= line.length() - 1) {
i = line.lastIndexOf("\\");
if (line.equals("")) {
return "";
}
if (i < 0 || i >= line.length() - 1) {
return line;
}
}
return line.substring(i + 1, line.length() - 1);
} private String getKey(String line) { // 從描述字元串中分離出欄位名
if (line == null) {
return "";
}
int i = line.indexOf("name=");
line = line.substring(i + 5).trim();
return line.substring(1, line.length() - 1);
} public static String[] split(String strOb, String mark) {
if (strOb == null) {
return null;
}
StringTokenizer st = new StringTokenizer(strOb, mark);
ArrayList<String> tmp = new ArrayList<String>();
while (st.hasMoreTokens()) {
tmp.add(st.nextToken());
}
String[] strArr = new String[tmp.size()];
for (int i = 0; i < tmp.size(); i++) {
strArr[i] = (String) tmp.get(i);
}
return strArr;
}
}

❷ java怎麼上傳圖片

  1. 使用Commons-FileUpload 上傳文件

  2. Struts 的文件上傳

  3. jspSmartUpload上傳文件

❸ java圖片上傳方案

給你簡單的描述一下:上傳的圖片保存在WebRoot下,在WebRoot下建立專門保存的文件夾,每上傳一張圖片數據保存圖片的路徑以及圖片的一些其他信息,你說的讀取直接查出路徑,用<img src="路徑">即可顯示

❹ java上傳圖片

/**
*
*/
package net.hlj.chOA.action;

import java.io.File;
import java.io.UnsupportedEncodingException;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.hlj.chOA..DangAnDao;
import net.hlj.chOA..LogDao;
import net.hlj.chOA..ZiYuanDao;
import net.hlj.chOA.model.DangAn;
import net.hlj.chOA.model.User;
import net.hlj.chOA.model.ZiYuan;
import net.hlj.chOA.util.GetId;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

/**
* @author lcy
*
*/
public class ZiYuanAction extends DispatchAction {

public ActionForward addZiYuan(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
String title = "";
String fileupload="";
SimpleDateFormat fmt = new SimpleDateFormat("yyMMddhhssmmSSS");
Date now = new Date();
String date = fmt.format(now);
String filePath = request.getSession().getServletContext().getRealPath(
"/");
String uploadPath = filePath + "upload\\uploadZiYuan\\";
String tempPath = filePath + "upload\\uploadZiYuan\\" + "uploadTemp";
String suffix = null;
if (!new File(uploadPath).isDirectory()) {
new File(uploadPath).mkdirs();
}
DiskFileItemFactory factory = new DiskFileItemFactory();
factory.setSizeThreshold(4194304);// 設置初始化內存,如果上傳的文件超過該大小,將不保存到內存,而且硬碟中(單位:byte)
File fileTemp = new File(tempPath);// 建立臨時目錄
fileTemp.mkdir();
factory.setRepository(fileTemp);
ServletFileUpload upload = new ServletFileUpload(factory);
upload.setSizeMax(4194304);// 設置客戶端最大上傳,-1為無限大(單位:byte)
try {
List<FileItem> items = upload.parseRequest(request);
Iterator<FileItem> i = items.iterator();
String[] rightType = {".gif", ".jpeg", ".doc", ".xls",
".pdf", ".txt", ".rar" };
while (i.hasNext()) {
FileItem fi = (FileItem) i.next();
if (fi.isFormField()) {
try {
if (fi.getFieldName().equals("title")) {
title = fi.getString("UTF-8");
}
} catch (UnsupportedEncodingException e) {
log.error(e.getMessage(), e);
}
} else {
String fileName = fi.getName();
int l = fileName.length();
if (!fileName.equals("")) {
int pos = fileName.lastIndexOf(".");
// suffix = fileName.substring(l - 4, l);
suffix = fileName.substring(pos, l);

boolean result = false;
String ext = fileName.substring(fileName
.lastIndexOf("."));
for (int j = 0; j < rightType.length; j++) {
if (ext.toLowerCase().equals(rightType[j])) {
result = true;
break;
}
}
if (!result) {
request.setAttribute("error", "上傳文件類型有誤!");
return mapping.findForward("addZiyuan");
}

// if (!suffix.equalsIgnoreCase(".jpg") &&
// !suffix.equalsIgnoreCase(".gif")
// && !suffix.equalsIgnoreCase(".png") &&
// !suffix.equalsIgnoreCase(".bmp")) {
// request.setAttribute("message", "上傳文件類型有誤!");
// return mapping.findForward("danganList");
// }
if (fileName != null) {
File savedFile = new File(uploadPath, date + suffix);
try {
fi.write(savedFile);
fileupload = "upload/uploadZiYuan/" + date
+ suffix;
} catch (Exception e) {
log.error(e.getMessage(), e);
}
}
}
}
}
} catch (FileUploadException e) {
log.error(e.getMessage(), e);
}
ZiYuan zy=new ZiYuan();
zy.setTitle(title);
zy.setUpload(fileupload);
ZiYuanDao zyDao=new ZiYuanDao();
try {
zyDao.addZiYuan(zy, this.servlet.getServletContext());
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
LogDao lDao=new LogDao();
//刪除操做添加日誌 isDel 0是增加,1是刪除,2是修改,3是審批
User user1 =(User)request.getSession().getAttribute("userBean");
String ip=request.getLocalAddr();
int id=GetId.getId("ziyuan", this.servlet.getServletContext());
try {
lDao.addLogMe("ziyuan", id , ip, user1.getName(), user1.getId(), 0, this.servlet.getServletContext());
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return mapping.findForward("ziyuanList");
}

public ActionForward deleteZiYuan(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
String[] ids= request.getParameterValues("id");
ZiYuanDao zyDao=new ZiYuanDao();
LogDao lDao=new LogDao();
for(int i=0;i<ids.length;i++){
try {
zyDao.deleteZiYuan(Integer.parseInt(ids[i]), this.servlet.getServletContext());
} catch (NumberFormatException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
User user =(User)request.getSession().getAttribute("userBean");
String ip=request.getLocalAddr();
try {
lDao.addLogMe("tbl_users",Integer.parseInt(ids[i]), ip, user.getName(), user.getId(), 1, this.servlet.getServletContext());
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}
return mapping.findForward("ziyuanList");
}

public ActionForward updateZiYuan(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) throws NumberFormatException, SQLException {
String id="";
String title = "";
String fileupload="";
SimpleDateFormat fmt = new SimpleDateFormat("yyMMddhhssmmSSS");
Date now = new Date();
String date = fmt.format(now);
String filePath = request.getSession().getServletContext().getRealPath(
"/");
String uploadPath = filePath + "upload\\uploadZiYuan\\";
String tempPath = filePath + "upload\\uploadZiYuan\\" + "uploadTemp";
String suffix = null;
if (!new File(uploadPath).isDirectory()) {
new File(uploadPath).mkdirs();
}
DiskFileItemFactory factory = new DiskFileItemFactory();
factory.setSizeThreshold(4194304);// 設置初始化內存,如果上傳的文件超過該大小,將不保存到內存,而且硬碟中(單位:byte)
File fileTemp = new File(tempPath);// 建立臨時目錄
fileTemp.mkdir();
factory.setRepository(fileTemp);
ServletFileUpload upload = new ServletFileUpload(factory);
upload.setSizeMax(4194304);// 設置客戶端最大上傳,-1為無限大(單位:byte)
try {
List<FileItem> items = upload.parseRequest(request);
Iterator<FileItem> i = items.iterator();
String[] rightType = {".gif", ".jpeg", ".doc", ".xls",
".pdf", ".txt", ".rar" };
while (i.hasNext()) {
FileItem fi = (FileItem) i.next();
if (fi.isFormField()) {
try {
if (fi.getFieldName().equals("title")) {
title = fi.getString("UTF-8");
}
if (fi.getFieldName().equals("id")) {
id = fi.getString("UTF-8");
}
} catch (UnsupportedEncodingException e) {
log.error(e.getMessage(), e);
}
} else {
String fileName = fi.getName();
int l = fileName.length();
if (!fileName.equals("")) {
int pos = fileName.lastIndexOf(".");
// suffix = fileName.substring(l - 4, l);
suffix = fileName.substring(pos, l);

boolean result = false;
String ext = fileName.substring(fileName
.lastIndexOf("."));
for (int j = 0; j < rightType.length; j++) {
if (ext.toLowerCase().equals(rightType[j])) {
result = true;
break;
}
}
if (!result) {
request.setAttribute("error", "上傳文件類型有誤!");
request.setAttribute("id", id);
return mapping.findForward("selectZiyuan");
}

// if (!suffix.equalsIgnoreCase(".jpg") &&
// !suffix.equalsIgnoreCase(".gif")
// && !suffix.equalsIgnoreCase(".png") &&
// !suffix.equalsIgnoreCase(".bmp")) {
// request.setAttribute("message", "上傳文件類型有誤!");
// return mapping.findForward("danganList");
// }
if (fileName != null) {
File savedFile = new File(uploadPath, date + suffix);
try {
fi.write(savedFile);
fileupload = "upload/uploadZiYuan/" + date
+ suffix;
} catch (Exception e) {
log.error(e.getMessage(), e);
}
}
}else{
//這里寫如果用戶沒有重寫添加附件則保持原來的附件
ZiYuanDao zyDao = new ZiYuanDao();
ZiYuan zy=(ZiYuan)zyDao.selectZiYuanById(Integer.parseInt(id), this.servlet.getServletContext()).get(0);
fileupload=zy.getUpload();
}
}
}
} catch (FileUploadException e) {
log.error(e.getMessage(), e);
}
ZiYuan zy=new ZiYuan();
zy.setId(Integer.parseInt(id));
zy.setTitle(title);
zy.setUpload(fileupload);
ZiYuanDao zyDao=new ZiYuanDao();
try {
zyDao.updateZiYuan(zy, this.servlet.getServletContext());
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
LogDao lDao=new LogDao();
//刪除操做添加日誌 isDel 0是增加,1是刪除,2是修改,3是審批
User user1 =(User)request.getSession().getAttribute("userBean");
String ip=request.getLocalAddr();
try {
lDao.addLogMe("ziyuan", Integer.parseInt(id) , ip, user1.getName(), user1.getId(), 2, this.servlet.getServletContext());
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

return mapping.findForward("ziyuanList");
}

}

❺ 怎麼用Java實現圖片上傳

下面這是servlet的內容:
package demo;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.ProgressListener;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

public class DemoServlet extends HttpServlet {

private static final String UPLOAD_DIRECTORY = "upload";
private static final int MEMORY_THRESHOLD = 1024 * 1024 * 3; // 3MB
private static final int MAX_FILE_SIZE = 1024 * 1024 * 40; // 40MB
private static final int MAX_REQUEST_SIZE = 1024 * 1024 * 50; // 50MB

protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
DiskFileItemFactory factory=new DiskFileItemFactory();
ServletFileUpload sfu=new ServletFileUpload(factory);
sfu.setHeaderEncoding("UTF-8");
sfu.setProgressListener(new ProgressListener() {

public void update(long pBytesRead, long pContentLength, int pItems) {
System.out.println("文件大小為:"+pContentLength+",當前已處理:"+pBytesRead);

}
});
//判斷提交上來的數據是否是上傳表單的數據
if(!ServletFileUpload.isMultipartContent(request)){
PrintWriter writer= response.getWriter();
writer.println("Error:表單必須包含 enctype=multipart/form-data");
writer.flush();
return;
}
factory.setSizeThreshold(MEMORY_THRESHOLD);
//設置臨時儲存目錄
factory.setRepository(new File(System.getProperty("java.io.tmpdir")));
//設置最大文件上傳值
sfu.setFileSizeMax(MAX_FILE_SIZE);
//設置最大請求值(包含文件和表單數據)
sfu.setSizeMax(MAX_REQUEST_SIZE);
String uploadpath=getServletContext().getRealPath("./")+ File.separator+UPLOAD_DIRECTORY;
File file=new File(uploadpath);
if(!file.exists()){
file.mkdir();
}

try {
List<FileItem> formItems = sfu.parseRequest(request);
if(formItems!=null&&formItems.size()>0){
for(FileItem item:formItems){
if(!item.isFormField()){
String fileName=new File(item.getName()).getName();
String filePath=uploadpath+File.separator+fileName;
File storeFile=new File(filePath);
System.out.println(filePath);
item.write(storeFile);
request.setAttribute("message", "文件上傳成功!");
}
}
}
} catch (Exception e) {
request.setAttribute("message", "錯誤信息:"+e.getMessage());
}
getServletContext().getRequestDispatcher("/demo.jsp").forward(request, response);
}

}

下面是jsp的內容,jsp放到webapp下,如果想放到WEB-INF下就把servlet里轉發的路徑改一下:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="demo.do" enctype="multipart/form-data" method="post">
<input type="file" name="file1" />
<%
String message = (String) request.getAttribute("message");
%>
<%=message%>
<input type="submit" value="提交"/>
</form>
</body>
</html>
這段代碼可以實現普通的文件上傳,有大小限制,上傳普通的圖片肯定沒問題,別的一些小的文件也能傳

❻ Java圖片上傳處理

upload.jsp
<%@ page contentType="text/html; charset=gb2312" language="java" import="java.util.*,com.jspsmart.upload.*" %>
<html>
<head>
<title>上傳文件 </title>

</head>
<body>
<form id="form1" name="msform" method="post" action="do_upload.jsp" enctype="multipart/form-data" onSubmit="return Check_Found(this);" target="iframe1">
<table width="50%" border="1" align="center">
<tr>
<td align="center"><input type="text" name="name" id="text1"></td>
</tr>
<tr>
<td align="center">產品說明:
<input type="file" name="file2" value=""/>
<iframe name="iframe1" style="display:none"> </iframe>
<input type="submit" name="Submit" value="上傳圖片" />
</td>
</tr>
</table>
</form>
</body>
</html>

do_upload.jsp
<%@ page contentType="text/html; charset=gb2312" language="java" import="java.util.*,com.jspsmart.upload.*" %>
<html>
<head>
<title>文件上傳處理頁面 </title>

</head>
<body>
<%
SmartUpload su=new SmartUpload();
su.initialize(pageContext);
su.upload();
String name;
int count=su.save("/upload",su.SAVE_VIRTUAL);
out.println(count+"個文件上傳成功! <br>");
for(int i=0;i <su.getFiles().getCount();i++)
{
com.jspsmart.upload.File file=su.getFiles().getFile(i);
if(file.isMissing()) continue;
String files=file.getFileName();
out.print("<script>window.parent.document.text1.value='../upload/"+file.getFileName()+"';</script>");
out.print("<script>alert('上傳成功!');</script>");
response.setHeader("Refresh","0;URL=addnewproce.jsp");
}
%>
</body>
</html>

還需要組件才行

❼ 請問用Java 如何實現圖片上傳功能

自己寫程序來上傳位元組流文件很難的,用SmartUpload.jar包吧,專門用於JSP上傳下載的,唯一缺點就是中文支持不太好,不過你可以改一下原程序的字元集就行了。上網搜,沒有找我!我給你發

❽ java實現上傳圖片功能

首先需要先要在資料庫中建立一個數據表格一遍存儲和查詢,在數據表格中的項中添加數據區和存儲路徑。數據區用來存儲圖片,在存儲時需要用到轉換功能,讓圖片轉換成數據流字元然後才能導入資料庫進行存儲,並且在輸入同時在同一語句中添加存儲路徑。
希望以上思路能讓你有點啟發

❾ Java中如何圖片非同步上傳

在java中要實現非同步上傳要提前做好准備,對於文件上傳,瀏覽器在上傳的過程中是將文件以流的形式提交到伺服器端的,如果直接使用Servlet獲取上傳文件的輸入流然後再解析裡面的請求參數是比較麻煩,所以一般選擇採用apache的開源工具common-fileupload這個文件上傳組件。
這個common-fileupload上傳組件的jar包可以去apache官網上面下載,也可以在struts的lib文件夾下面找到,struts上傳的功能就是基於這個實現的。
common-fileupload是依賴於common-io這個包的,所以還需要下載這個包。剩下的就是js文件的導入了,我導入了以下文件:
<script type="text/javascript" src="lib/Js/jquery.js"></script>
<script ltype="text/javascript" src="/js/ajaxfileupload.js"></script>

在頁面中的寫法:
div class="controls"><span class="btn green fileinput-button"><i class="icon-plus icon-white"></i>
<span>上傳照片</span>
<input id="fileToUpload" name="myfiles" type="file" onchange="upload()" title="上傳" /></span>
</div>function upload(){
$.ajaxFileUpload
(
{
url:'<%=basePath%>sysperson/uploadpic',
secureuri:false,
fileElementId:'fileToUpload',
dataType: 'text',
success: function (data, status)
{
document.all.mypic.src="<%=basePath%>uploads/" + data;
document.all.picpath.value = data;
}, error : function(data, status, e) {
alert(e);
}
});
}

❿ java怎樣實現圖片上傳功能

圖片不會那麼的保存到資料庫中。
而是把圖片放到文件系統,或者你指定的一個位置。
然後再資料庫中存放的為圖片的路徑

閱讀全文

與java圖片上傳相關的資料

熱點內容
壓縮因子定義 瀏覽:968
cd命令進不了c盤怎麼辦 瀏覽:214
葯業公司招程序員嗎 瀏覽:974
毛選pdf 瀏覽:659
linuxexecl函數 瀏覽:727
程序員異地戀結果 瀏覽:374
剖切的命令 瀏覽:228
干什麼可以賺錢開我的世界伺服器 瀏覽:290
php備案號 瀏覽:990
php視頻水印 瀏覽:167
怎麼追程序員的女生 瀏覽:487
空調外壓縮機電容 瀏覽:79
怎麼將安卓變成win 瀏覽:459
手機文件管理在哪兒新建文件夾 瀏覽:724
加密ts視頻怎麼合並 瀏覽:775
php如何寫app介面 瀏覽:804
宇宙的琴弦pdf 瀏覽:396
js項目提成計算器程序員 瀏覽:944
pdf光子 瀏覽:834
自拍軟體文件夾名稱大全 瀏覽:328