图片上传

var dfFile = document.getElementById('defaultFile');
var dfImg = document.getElementById('default_img');

$("#defaultFile").change(function(){
if(!dfFile.value.match(/.jpg|.gif|.png/i)){
$('#errorDefaultImage').html("文件类型必须是: .jpg, .gif 或者 .png !");
return;
}
if(dfFile.files){
dfImg.src = dfFile.files[0].getAsDataURL();
}
});


action
private file defalutFile;
private String defaultFileFileName;

string logoUrl = ServiceManager.agent.getPubVirtualGoodsService()
.uploadLogo(defaultFile, defaultFileFileName,
adminUserDto.getId());

/**
*
* @param parentPath
* @param file
* @param fileName
* @param businessCode
* 基因号对应的ID
* @param businessType
* 商家类型 Z系统管理员 C消费者 S实体店 网店 A广告商
* @return
* @See 上传文件
*/
public String uploadFile(String parentPath, File file, String fileName,
String businessCode, String businessType) {

StringBuffer upPath = new StringBuffer();
// 基础路径
upPath.append(AppConfig.get("SERVICE.PATH.UPLOAD.BASEPATH"));
StringBuffer savePath = new StringBuffer();
// 商家类型
savePath.append(businessType);
savePath.append("/");
// 基因号对应的ID
savePath.append(FileUtils.encrypt(Long.valueOf(businessCode)));
savePath.append("/");
// parentPath
savePath.append(parentPath);
savePath.append("/");
upPath.append(savePath);

Resource fileSystemRes = new FileSystemResource(upPath.toString());
int extPlace = fileName.split("\\.").length - 1;
String ext = null;
if (extPlace >= 0)
ext = fileName.split("\\.")[extPlace];
String storePath = filePathSelector.select(file.getName(), ext);
File out = null;
try {
out = fileSystemRes.createRelative(storePath).getFile();
if (!out.getParentFile().exists() && !out.getParentFile().mkdirs()) {
}

} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
saveToFile(file, out);
out = null;
}

return savePath + storePath;
}


private void saveToFile(File src, File dst) {
InputStream in = null;
OutputStream out = null;
try {
in = new BufferedInputStream(new FileInputStream(src), BUFFER_SIZE);
out = new BufferedOutputStream(new FileOutputStream(dst),
BUFFER_SIZE);
byte[] buffer = new byte[BUFFER_SIZE];
int len = 0;
while ((len = in.read(buffer)) > 0) {
out.write(buffer, 0, len);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (null != in) {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (null != out) {
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值