1 html 直接传参 jsp页面
var Lxlmc = "";//linux
var xtxh = "";//win
if(xtxh.substring(0,3)!="win"){
xlmc = Lxlmc;
}
2 文件分隔符linux与windows不同,用System.getProperty("file.separator")统一
private static final long serialVersionUID = -6525414170411696287L;
private static final String updpathdir = System.getProperty("file.separator")+"public"+System.getProperty("file.separator")+"images"+System.getProperty("file.separator")+"upload"+System.getProperty("file.separator");
private String filedir="zhct";//文件所在文件夹
private String name;//图片名字
private String suffix="png";//图片扩展名
private File file;//上传单个文件
private int width = 200;
private int height = 160;
private List multiFiles;//上传多个文件
private OrderManagerInf orderManager;
private int id;
private String rq;
//图片列表
public String getImgs() throws Exception {
HttpServletRequest request = ServletActionContext.getRequest();
String realpath = request.getSession( ).getServletContext( ).getRealPath( "/" )+updpathdir+filedir;
String[] fiename = null;//name存储file文件夹中的文件名
List list = new ArrayList<>();
File newfile = new File(realpath);
if(!newfile.exists()){
newfile.mkdirs();//如果目录不存在,新建
}else{
if (newfile.isDirectory()){//判断file是否是文件目录 若是返回TRUE
fiename = newfile.list();
Collections.addAll(list, fiename);
Collections.sort(list,Collator.getInstance(java.util.Locale.CHINA));
}
}
request.setAttribute("random", System.currentTimeMillis());
request.setAttribute("filedir", filedir);
request.setAttribute("fienames", list);
return "picList";
}
//上传图片,png,透明
public void updPngFile() throws Exception {//png,jpg图片,压缩,背景白的
HttpServletRequest request = ServletActionContext.getRequest();
HttpServletResponse response = ServletActionContext.getResponse();
response.setContentType("text/html;charset=utf-8");
PrintWriter out = response.getWriter();
String realpath = request.getSession( ).getServletContext( ).getRealPath( "/" )+updpathdir+filedir;
Map re = new HashMap<>();
try{
if(file == null){
throw new Exception(I18nUtil.getText("basicModule.zhct.pic.notexit", session.getAttribute("appLang")+""));
}
BufferedImage bi1 = ImageIO.read(file);
if(bi1 == null){
throw new Exception(I18nUtil.getText("basicModule.zhct.pic.geshi", session.getAttribute("appLang")+""));
}
if(file.length()>=500000){
throw new Exception(I18nUtil.getText("basicModule.zhct.pic.size", session.getAttribute("appLang")+""));
}
File newfile = new File(realpath);
if(!newfile.exists()){
newfile.mkdirs();//如果目录不存在,新建
}
String fileName = name+".png";
String srcImgPath = realpath+System.getProperty("file.separator")+fileName; //存成png格式
FileInputStream is = new FileInputStream(file);
BufferedImage sourceImg =ImageIO.read(new FileInputStream(file));
int width = sourceImg.getWidth();
int height = sourceImg.getHeight();
//获取图片流
ImageInputStream iis = ImageIO.createImageInputStream(is);
Iterator it = ImageIO.getImageReadersByFormatName(suffix);
ImageReader reader = it.next();
reader.setInput(iis,true) ;
ImageReadParam param = reader.getDefaultReadParam();
Rectangle rect = new Rectangle(0,0,width,height);
param.setSourceRegion(rect);
BufferedImage bi = reader.read(0,param);
ImageIO.write(bi, suffix, new File(srcImgPath));
re.put("fileName", fileName);
re.put("status", "success");
}catch (Exception e) {
logger.info("updPngFile fail...", e);
re.put("status", "exception");
re.put("message", e.getMessage());
}
// 写入列表 查询结果
out.println(JSON.toJSONString(re));
out.flush();
out.close();
}
//上传图片
public void updFile() throws Exception {//png,jpg图片,压缩,背景白的
HttpServletRequest request = ServletActionContext.getRequest();
HttpServletResponse response = ServletActionContext.getResponse();
response.setContentType("text/html;charset=utf-8");
PrintWriter out = response.getWriter();
String realpath = request.getSession( ).getServletContext( ).getRealPath( "/" )+updpathdir+filedir;
Map re = new HashMap<>();
try{
if(file == null){
throw new Exception("文件不存在!");
}
BufferedImage bi1 = ImageIO.read(file);
if(bi1 == null){
throw new Exception("此文件不为图片文件");
}
File newfile = new File(realpath);
if(!newfile.exists()){
newfile.mkdirs();//如果目录不存在,新建
}
Image srcFile = ImageIO.read(file);
String fileName = name+".png";
String srcImgPath = realpath+System.getProperty("file.separator")+fileName; //存成png格式
BufferedImage buffImg = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Graphics2D graphics = buffImg.createGraphics();
graphics.setBackground(new Color(255,255,255));
graphics.setColor(new Color(255,255,255));
graphics.fillRect(0, 0, width, height);
graphics.drawImage(srcFile.getScaledInstance(width, height, Image.SCALE_SMOOTH), 0, 0, null);
ImageIO.write(buffImg, "jpg", new File(srcImgPath));
re.put("fileName", fileName);
re.put("status", "success");
}catch (Exception e) {
logger.info("updFile fail...", e);
re.put("status", "exception");
re.put("message", e.getMessage());
}
// 写入列表 查询结果
out.println(JSON.toJSONString(re));
out.flush();
out.close();
}
//删除图片
public void delFile() throws Exception {
HttpServletRequest request = ServletActionContext.getRequest();
HttpServletResponse response = ServletActionContext.getResponse();
response.setContentType("text/html;charset=utf-8");
PrintWriter out = response.getWriter();
String realpath = request.getSession( ).getServletContext( ).getRealPath( "/" )+updpathdir+filedir;
Map re = new HashMap<>();
try{
String srcImgPath = realpath+System.getProperty("file.separator")+name; //存成png格式
File f = new File(srcImgPath);
if(f.exists()){
f.delete();
}
re.put("status", "success");
}catch (Exception e) {
logger.info("delFile fail...", e);
re.put("status", "exception");
re.put("message", e.getMessage());
}
// 写入列表 查询结果
out.println(JSON.toJSONString(re));
out.flush();
out.close();
}
//改变图片名称
public void editFile() throws Exception {
HttpServletRequest request = ServletActionContext.getRequest();
HttpServletResponse response = ServletActionContext.getResponse();
response.setContentType("text/html;charset=utf-8");
PrintWriter out = response.getWriter();
String realpath = request.getSession( ).getServletContext( ).getRealPath( "/" )+updpathdir+filedir;
Map re = new HashMap<>();
try{
String oldPath = realpath+System.getProperty("file.separator")+name; //存成png格式
File f = new File(oldPath);
String newPath = realpath+System.getProperty("file.separator")+request.getParameter("newName")+".png";
f.renameTo(new File(newPath)); //改名
re.put("status", "success");
}catch (Exception e) {
logger.info("editFile fail...", e);
re.put("status", "exception");
re.put("message", e.getMessage());
}
// 写入列表 查询结果
out.println(JSON.toJSONString(re));
out.flush();
out.close();
}
pageEncoding="UTF-8"%>
window.onload = function() {
hideLeft();
};
function uploadFile(obj) {
var file = $("#file").val();
var name = getFileName(file);
var index1=file.lastIndexOf(".");
var index2=file.length;
var suffix=file.toLowerCase().substring(index1+1,index2);//后缀名
if(suffix!="png" & suffix!="jpg" & suffix!="jpeg"){
Wg.alert(basicModule_zhct.resourceBundle.Error.geshi);
return false;
}
var reg = /[\u4e00-\u9fa5]/g;
if(reg.test(name)){
Wg.alert(basicModule_zhct.resourceBundle.Error.ischar);
return false;
}
$.ajaxFileUpload({
url : "pic!updFile.do",
secureuri : false,// 一般设置为false
fileElementId : "file",// 文件上传表单的id
dataType : 'json',// 返回值类型 一般设置为json
data: {'name': name,'filedir': '${filedir}','width':${width},'height':${height}},
success : function(data) // 服务器成功响应处理函数
{
var re = eval("(" + data + ")");
var timestamp=new Date().getTime();//id
if(re.status == 'success'){
var html = '
+'
+''
+'
$("#imgs").append(html);
}else{
Wg.alert(re.message);
}
},
error : function(data)// 服务器响应失败处理函数
{
Wg.alert(basicModule_zhct.resourceBundle.Error.upload);
}
});
return false;
}
//获取图片名称
function getFileName(o){
var pos=o.lastIndexOf("\\");
var fileName = o.substring(pos+1);
pos=fileName.lastIndexOf(".");
return fileName.substring(0,pos);
}
function del(name,tar){
Wg.confirm(basicModule_zhct.resourceBundle.Confirm.del, function() {
$.ajax({
type: 'POST',
url: 'pic!delFile.do',
data: {'name': name,'filedir': '${filedir}'},
success : function(data) // 服务器成功响应处理函数
{
var re = eval("(" + data + ")");
if(re.status == 'success'){
$(tar).parent().parent().remove();
}else{
Wg.alert(re.message);
}
},
error : function(data)// 服务器响应失败处理函数
{
Wg.alert(basicModule_zhct.resourceBundle.Error.del);
}
});
});
}
function toInput(id){
if($("#"+id).attr("disabled")=="disabled"){
$("#"+id).css('background-color','#FFFFFF');
$("#"+id).attr("disabled",false);
$("#"+id).focus();
}
}
function edit(tar,name){
var newName = $(tar).val();
//中文判断
var reg = /[\u4e00-\u9fa5]/g;
if(reg.test(newName)){
Wg.alert(basicModule_zhct.resourceBundle.Error.ischar);
console.log(newName.length);
$(tar).val(name.substring(0,name.length-4));
return false;
}
$(tar).attr("disabled",true);
$(tar).css('background-color','#e1e1e1');
//修改
$.ajax({
type: 'POST',
url: 'pic!editFile.do',
data: {'name': name,'newName':newName,'filedir': '${filedir}'},
success : function(data) // 服务器成功响应处理函数
{
var re = eval("(" + data + ")");
if(re.status != 'success'){
Wg.alert(re.message);
}
},
error : function(data)// 服务器响应失败处理函数
{
Wg.alert(basicModule_zhct.resourceBundle.Error.upload);
}
});
}
.fileInput{width: 150px;height: 150px;overflow: hidden;opacity: 0;filter:alpha(opacity=0);cursor:pointer;}
.divKJ{border: 1px solid #e1e1e1;width: 150px;height: 150px;float: left;margin-left: 30px;margin-top: 30px}
.divIMG{width:150px;height: 120px}
" src="${ctx}/public/extjs/resources/icons/fam/editl.png" οnclick="toInput('kd${s.index}')" style="cursor:pointer;"/>
" src="${ctx}/public/extjs/resources/icons/fam/dell.png" οnclick="del('${item}',this)" style="cursor:pointer;"/>