當前位置:首頁 » 文件管理 » jsp上傳圖片插件

jsp上傳圖片插件

發布時間: 2022-09-20 10:12:54

A. jsp中使用jquery的ajaxfileupload插件怎麼實現非同步上傳

ajaxfileupload實現非同步上傳的完整例子:
JSP頁面中引入的script代碼:
<script>
function ajaxFileUpload()
{
$("#loading").ajaxStart(function(){
$(this).show();
})//開始上傳文件時顯示一個圖片
.ajaxComplete(function(){
$(this).hide();
});//文件上傳完成將圖片隱藏起來

$.ajaxFileUpload({
url:'AjaxImageUploadAction.action',//用於文件上傳的伺服器端請求地址
secureuri:false,//一般設置為false
fileElementId:'imgfile',//文件上傳空間的id屬性 <input type="file" id="imgfile" name="file" />
dataType: 'json',//返回值類型 一般設置為json
success: function (data, status) //伺服器成功響應處理函數
{
alert(data.message);//從伺服器返回的json中取出message中的數據,其中message為在struts2中定義的成員變數

if(typeof(data.error) != 'undefined')
{
if(data.error != '')
{
alert(data.error);
}else
{
alert(data.message);
}
}
},
error: function (data, status, e)//伺服器響應失敗處理函數
{
alert(e);
}
}
)

return false;
}
</script>
struts.xml配置文件中的配置方法:
<struts>
<package name="struts2" extends="json-default">
<action name="AjaxImageUploadAction" class="com.test.action.ImageUploadAction">
<result type="json" name="success">
<param name="contentType">text/html</param>
</result>
<result type="json" name="error">
<param name="contentType">text/html</param>
</result>
</action>
</package>
</struts>
上傳處理的Action ImageUploadAction.action
package com.test.action;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.Arrays;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
@SuppressWarnings("serial")
public class ImageUploadAction extends ActionSupport {

private File imgfile;
private String imgfileFileName;
private String imgfileFileContentType;
private String message = "你已成功上傳文件";
public File getImgfile() {
return imgfile;
}
public void setImgfile(File imgfile) {
this.imgfile = imgfile;
}
public String getImgfileFileName() {
return imgfileFileName;
}
public void setImgfileFileName(String imgfileFileName) {
this.imgfileFileName = imgfileFileName;
}
public String getImgfileFileContentType() {
return imgfileFileContentType;
}
public void setImgfileFileContentType(String imgfileFileContentType) {
this.imgfileFileContentType = imgfileFileContentType;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}

@SuppressWarnings("deprecation")
public String execute() throws Exception {

String path = ServletActionContext.getRequest().getRealPath("/upload/mri_img_upload");
String[] imgTypes = new String[] { "gif", "jpg", "jpeg", "png","bmp" };
try {
File f = this.getImgfile();

String fileExt = this.getImgfileFileName().substring(this.getImgfileFileName().lastIndexOf(".") + 1).toLowerCase();
/*
if(this.getImgfileFileName().endsWith(".exe")){
message="上傳的文件格式不允許!!!";
return ERROR;
}*/
/**
* 檢測上傳文件的擴展名是否合法
* */
if (!Arrays.<String> asList(imgTypes).contains(fileExt)) {
message="只能上傳 gif,jpg,jpeg,png,bmp等格式的文件!";
return ERROR;
}

FileInputStream inputStream = new FileInputStream(f);
FileOutputStream outputStream = new FileOutputStream(path + "/"+ this.getImgfileFileName());
byte[] buf = new byte[1024];
int length = 0;
while ((length = inputStream.read(buf)) != -1) {
outputStream.write(buf, 0, length);
}
inputStream.close();
outputStream.flush();
} catch (Exception e) {
e.printStackTrace();
message = "文件上傳失敗了!!!!";
}
return SUCCESS;
}
}

B. jsp上傳圖片,選擇圖片後先不保存只顯示在之前的頁面,然後下面填寫了其他的信息後在一起保存到資料庫

<html>
<head>

<script type="text/javascript">
function show()
{
var img = document.getElementById("file").value;
document.getElementById("photo").src =img;
}
</script>
</head>
<body>
<input type="file" onchange="show()" id="file"></input>
<img id="photo" src="" style="width:400px;height:300px;" ></img>
</body>
</html>
試試這段代碼,不過在有的瀏覽器裡面無效。

C. jsp頁面實現圖片預覽,截取和上傳

比較常用,而且簡單易用的jquery-uploadify插件,支持帶進度的多線程上傳

用到的是flash的跨域上傳模型,這里不用深究

基本文件大致包括

jquery-x.x.x.js

jquery.uploadify.x.js

uploadify.swf

uploadify.css


使用方式:

	$(function(){
$("#fileId").uploadify({
width:42,
height:32,
swf:'js/uploadify.swf',
uploader:'upload.do;jsessionid=<%=session.getId()%>',
buttonImage:'image/movetophone_white.png',
fileSizeLimit:2048,
fileObjName:"imgFile",
method:'post',
removeCompleted:true,
fileTypeExts:"*.gif;*.jpg;*.png;*.jpeg;*.bmp",
onSelectError:function(file,errorCode,errorMsg){
alert("文件過大");
},
onUploadStart:function(file){

},
onUploadSuccess:function(file,data,response){
alert("上傳完成");
},
onUploadError:function(file,errorCode,errorMsg){
alert(errorMsg);
}
});
});
<inputtype="file"id="fileId"/>


另,工程中需要引入commons-fileupload的包。

D. jsp上傳及顯示照片

<form action="pages/news/NewsServlet?status=insert" method="post" enctype="multipart/form-data">
新聞圖片:
<input type="file" name="img">
<br />
<input type="submit" value="添加">
</form>
後台代碼
public void insert(HttpServletRequest request, HttpServletResponse response)
throws Exception {
SmartUpload smart = new SmartUpload();
smart.initialize(super.getServletConfig(), request, response);
smart.upload();

File img = smart.getFiles().getFile(0);
String fileName = null;
if (img != null && img.getSize() > 0) {
fileName = UUID.randomUUID().toString() + "." + img.getFileExt();

img.saveAs("/upload/" + fileName);
}

String title = smart.getRequest().getParameter("title");
String content = smart.getRequest().getParameter("content");

News news = new News();
news.setTitle(title);
news.setContent(content);
news.setImg(fileName);
news.setPostDate(new Date());

boolean flag = ServiceFactory.getINewsServiceInstance().insert(news);

if (flag) {
request.setAttribute("message", "新聞添加成功");
} else {
request.setAttribute("message", "新聞添加失敗");
}
request.setAttribute("url", "pages/news/NewsServlet?status=list");

request.getRequestDispatcher("/pages/forward.jsp").forward(request,
response);
}
}

E. ueditor1.4.3 jsp版本在上傳圖片時出現「後端配置項沒有正常載入,上傳插件不能正常使用!」

關於這個錯誤,富文本框無法上傳文件的解決方案:
1、確認config.json裡面的 imagePathFormat 這個路徑正確;
2、確認在web-inf下面的lib中已導入Ueditor的jar包和文件上傳相關的jar包。

F. jsp中使用jquery的ajaxfileupload插件怎麼實現非同步上傳

JSP頁面中引入的script代碼
<script>
function
ajaxFileUpload()
{
$("#loading").ajaxStart(function(){
$(this).show();
})//開始上傳文件時顯示一個圖片
.ajaxComplete(function(){
$(this).hide();
});//文件上傳完成將圖片隱藏起來
$.ajaxFileUpload({
url:'AjaxImageUploadAction.action',//用於文件上傳的伺服器端請求地址
secureuri:false,//一般設置為false
fileElementId:'imgfile',//文件上傳空間的id屬性
<input
type="file"
id="imgfile"
name="file"
/>
dataType:
'json',//返回值類型
一般設置為json
success:
function
(data,
status)
//伺服器成功響應處理函數
{
alert(data.message);//從伺服器返回的json中取出message中的數據,其中message為在struts2中定義的成員變數
if(typeof(data.error)
!=
'undefined')
{
if(data.error
!=
'')
{
alert(data.error);
}else
{
alert(data.message);
}
}
},
error:
function
(data,
status,
e)//伺服器響應失敗處理函數
{
alert(e);
}
}
)
return
false;
}
</script>
struts.xml配置文件中的配置方法:
<struts>
<package
name="struts2"
extends="json-default">
<action
name="AjaxImageUploadAction"
class="com.test.action.ImageUploadAction">
<result
type="json"
name="success">
<param
name="contentType">text/html</param>
</result>
<result
type="json"
name="error">
<param
name="contentType">text/html</param>
</result>
</action>
</package>
</struts>
上傳處理的Action
ImageUploadAction.action
package
com.test.action;
import
java.io.File;
import
java.io.FileInputStream;
import
java.io.FileOutputStream;
import
java.util.Arrays;
import
org.apache.struts2.ServletActionContext;
import
com.opensymphony.xwork2.ActionSupport;
@SuppressWarnings("serial")
public
class
ImageUploadAction
extends
ActionSupport
{
private
File
imgfile;
private
String
imgfileFileName;
private
String
imgfileFileContentType;
private
String
message
=
"你已成功上傳文件";
public
File
getImgfile()
{
return
imgfile;
}
public
void
setImgfile(File
imgfile)
{
this.imgfile
=
imgfile;
}
public
String
getImgfileFileName()
{
return
imgfileFileName;
}
public
void
setImgfileFileName(String
imgfileFileName)
{
this.imgfileFileName
=
imgfileFileName;
}
public
String
getImgfileFileContentType()
{
return
imgfileFileContentType;
}
public
void
setImgfileFileContentType(String
imgfileFileContentType)
{
this.imgfileFileContentType
=
imgfileFileContentType;
}
public
String
getMessage()
{
return
message;
}
public
void
setMessage(String
message)
{
this.message
=
message;
}
@SuppressWarnings("deprecation")
public
String
execute()
throws
Exception
{
String
path
=
ServletActionContext.getRequest().getRealPath("/upload/mri_img_upload");
String[]
imgTypes
=
new
String[]
{
"gif",
"jpg",
"jpeg",
"png","bmp"
};
try
{
File
f
=
this.getImgfile();
String
fileExt
=
this.getImgfileFileName().substring(this.getImgfileFileName().lastIndexOf(".")
+
1).toLowerCase();
/*
if(this.getImgfileFileName().endsWith(".exe")){
message="上傳的文件格式不允許!!!";
return
ERROR;
}*/
/**
*
檢測上傳文件的擴展名是否合法
*
*/
if
(!Arrays.<String>
asList(imgTypes).contains(fileExt))
{
message="只能上傳
gif,jpg,jpeg,png,bmp等格式的文件!";
return
ERROR;
}
FileInputStream
inputStream
=
new
FileInputStream(f);
FileOutputStream
outputStream
=
new
FileOutputStream(path
+
"/"+
this.getImgfileFileName());
byte[]
buf
=
new
byte[1024];
int
length
=
0;
while
((length
=
inputStream.read(buf))
!=
-1)
{
outputStream.write(buf,
0,
length);
}
inputStream.close();
outputStream.flush();
}
catch
(Exception
e)
{
e.printStackTrace();
message
=
"文件上傳失敗了!!!!";
}
return
SUCCESS;
}
}
轉載,僅供參考。

G. jquery 上傳文件插件uploadify jsp版本

寫一個servlet,看這里
http://www.javaeye.com/topic/376101

H. struts2如何上傳圖片並在同一jsp中顯示出來

//action的關鍵代碼
import com.xt.gyz.facelook.service.FacesService;
public class ShowPic extends ActionSupport {

private InputStream is;
private Integer id;
private FacesService facesService;

public Integer getId() {
return id;
}

public void setId(Integer id) {
this.id = id;
}

public void setFacesService(FacesService facesService) {
this.facesService = facesService;
}

public InputStream getIs() {
return is;
}

public void setIs(InputStream is) {
this.is = is;
}

@Override
public String execute() throws Exception {
Faces face=facesService.findById(id);
is=new ByteArrayInputStream(face.getPic());
// FileOutputStream fos=new FileOutputStream(new File("E:/body.jpg"));
// System.out.println(fos);
// System.out.println("face:"+face.getPic().length);
// fos.write(face.getPic());
// fos.flush();
// fos.close();
return super.execute();
}
}
//不要忘記struts.xml配置
......
<action name="showPic" class="showPicAction">
<result type="stream">
<param name="inputName">is</param>
<param name="contectType">image/jpg</param>
</result>
</action>
.....

這樣就可以啦

I. extjs 3.4中 怎麼給htmlEdit添加圖片插件 實現圖片上傳功能

首先要使用extjs自帶的HTMLEditor,然後在原有的工具條上添加一個圖片按鈕,點擊這個圖片按鈕要彈出窗口,這個窗口負責實現上傳功能,實現上傳後,要將上傳的圖片路徑添加到HTMLEditor的游標處,並且要以<IMG></IMG>的方式,這樣HTMLEditor才能解析出來。實現代碼如下:

前台JSP頁面



fieldLabel : '商品特性',
id : 'shopSp.spTxms',

name : 'shopSp.spTxms',
xtype : 'StarHtmleditor',
anchor : '93%'


這其中引用了StarHtmleditor,StarHtmleditor.js的代碼如下,直接將代碼復制下來,然後新建個JS,全復制進去就行了。


var HTMLEditor = Ext.extend(Ext.form.HtmlEditor, {
addImage : function() {
var editor = this;
var imgform = new Ext.FormPanel({
region : 'center',
labelWidth : 55,
frame : true,
bodyStyle : 'padding:5px 5px 0',
autoScroll : true,
border : false,
fileUpload : true,
items : [{
xtype : 'textfield',
fieldLabel : '選擇文件',
id : 'UserFile',
name : 'UserFile',
inputType : 'file',
allowBlank : false,
blankText : '文件不能為空',
anchor : '90%'
}],
buttons : [{
text : '上傳',
handler : function() {
if (!imgform.form.isValid()) {return;}
imgform.form.submit({
waitMsg : '正在上傳......',
url : 'HTMLEditorAddImgCommonAction.action',
success : function(form, action) {
var element = document.createElement("img");
element.src = action.result.fileURL;
if (Ext.isIE) {
editor.insertAtCursor(element.outerHTML);
} else {
var selection = editor.win.getSelection();
if (!selection.isCollapsed) {
selection.deleteFromDocument();
}
selection.getRangeAt(0).insertNode(element);
}
//win.hide();//原始方法,但只能傳一個圖片
//更新後的方法
form.reset();
win.close();
},
failure : function(form, action) {
form.reset();
if (action.failureType == Ext.form.Action.SERVER_INVALID)
Ext.MessageBox.alert('警告','上傳失敗',action.result.errors.msg);
}
});
}
}, {
text : '關閉',
handler : function() {
win.close(this);
}
}]
})

var win = new Ext.Window({
title : "上傳圖片",
width : 300,
height : 200,
modal : true,
border : false,
iconCls : "picture.png",
layout : "fit",
items : imgform

});
win.show();
},
createToolbar : function(editor) {
HTMLEditor.superclass.createToolbar.call(this, editor);
this.tb.insertButton(16, {
cls : "x-btn-icon",
icon : "picture.png",
handler : this.addImage,
scope : this
});
}
});
Ext.reg('StarHtmleditor', HTMLEditor);

JS的第一句var HTMLEditor = Ext.extend(Ext.form.HtmlEditor, 網上是沒有var的,不用var不知道為什麼總是報錯,另外JS一定要與JSP的編碼方式一致,要不然報莫名其妙的錯誤,而且錯誤都沒有顯示。

後台java代碼



/****
* HTMLEditor增加上傳圖片功能:
* 1、上傳圖片後,需要將圖片的位置及圖片的名稱返回給前台HTMLEditor
* 2、前台HTMLEditor根據返回的值將圖片顯示出來
* 3、進行統一保存
* @param 上傳圖片功能
* @return JSON結果
* @throws IOException
*/
public void HTMLEditorAddImg() throws IOException {
if(!"".equals(UserFile) && UserFile != null && UserFile.length() > 0){
File path = ImportImg(UserFile, "jpg");
UserFilePath = "../" + path.toString().replaceAll("\\", "/").substring(path.toString().replaceAll("\\", "/").indexOf("FileImg"));
}
this.getResponse().setContentType("text/html");
this.getResponse().getWriter().write("{success:'true',fileURL:'" + UserFilePath + "'}");
}


特別要注意的是路徑問題,路徑問題主要有2點需要注意:

1、前台頁面引用StarHtmleditor.js的路徑一定要正確;

2、Htmleditor上傳的圖片路徑一定要改,因為上傳之後圖片路徑變為http://localhost:8080/,在正常使用中圖片不顯示,要將該地址替換為伺服器的IP地址;替換方法如下:


//獲取本地IP地址,因為extjs的htmleditor上傳的照片路徑有問題,需要將路徑替換為本機IP地址
InetAddress inet = InetAddress.getLocalHost();
shopSp.setSpTxms(shopSp.getSpTxms().replace("localhost", inet.getHostAddress().toString()));

這樣基本就完成了這個HTMLEditor上傳圖片功能。

如圖:

J. fancyzoom 在jsp中怎麼使用

fancyzoom是一個js寫的圖片縮放插件,使用方法如下:
FancyZoom的用法是在網頁載入的時候,就執行它的特效載入函數:setupZoom();這樣只能針對圖片已經在網頁中的這種情況,如果類似上傳,圖片是用戶後來傳上去的,那麼就需要在圖片上傳完之後再執行一次特效渲染函數:setupZoom();
使用方法如下:
1、引入js文件:FancyZoomHTML.js
2、在需要縮放的圖片上寫如下代碼:
var inSpinbox = document.getElementById('ZoomSpin');

if (inSpinbox) {return;}
inSpinbox = document.createElement("div");

熱點內容
使命召喚生化武器怎麼配置 發布:2024-10-10 13:13:15 瀏覽:490
磁碟陣列怎麼配置管理口 發布:2024-10-10 13:10:05 瀏覽:187
如何撤銷自己的配置 發布:2024-10-10 12:41:47 瀏覽:687
win7無法復制文件夾 發布:2024-10-10 12:40:11 瀏覽:147
文章存儲結構 發布:2024-10-10 12:40:11 瀏覽:881
添加多個FTP網站的方法有哪些 發布:2024-10-10 12:03:03 瀏覽:842
表格怎麼調用網頁資料庫資料庫數據 發布:2024-10-10 11:37:33 瀏覽:657
海力壓縮機 發布:2024-10-10 11:32:26 瀏覽:526
洗過的海帶存儲方法 發布:2024-10-10 11:19:00 瀏覽:241
64H的源碼 發布:2024-10-10 11:06:01 瀏覽:153