希望软件图片名称修改为 软件编号.图片格式 存储
我在Uploadify的后台serverlet进行修改,用String appNo = request.getParameter("appNo");获取appNo,让后用appNo给文件名赋值,但是测试发现appNo总是为空的。uploadify上传图片在点击“上传图片”的时候就上传到服务器了,并不是在提交整个页面的时候。是不是必须提交页面才能获取appNo呢?求解。
1 个解决方案
#1
页面
<script type="text/javascript">
$(document).ready(function() {
$("#uploadify").uploadify({
'uploader' : '<%=request.getContextPath()%>/uploadify.swf',
'script' : '<%=basePath %>/UploadController',
'cancelImg' : '<%=request.getContextPath()%>/images/cancel.png',
'queueID' : 'fileQueue',
'auto' : false,
'multi' : true,
'buttonText' : 'Browse file',
'fileExt': '*.jpg;*.gif;*.jpeg;*.png;*.bmp',
'fileDesc': '请选择jpg,gif,jpeg,png,bmp格式',
'queueSizeLimit' : 5,
onComplete: function (evt, queueID, fileObj, response, data) {
$('#appImg').val($('#appImg').val()+response+',');
},
onError: function(a, b, c, d) {
alert("文件上传失败");
}
});
});
</script>
后台servlet
public class UploadController extends HttpServlet {
private static final long serialVersionUID = 1L;
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setCharacterEncoding("utf-8");
String savePath = request.getSession().getServletContext().getRealPath(File.separator+"AppImgPackage"+File.separator);
DiskFileItemFactory fac = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(fac);
upload.setHeaderEncoding("utf-8");
List fileList = null;
try {
fileList = upload.parseRequest(request);
} catch (FileUploadException ex) {
ex.printStackTrace();
}
Iterator<FileItem> it = fileList.iterator();
String name = "";
String extName = "";
String category = "";
String appNo = request.getParameter("appNo");
String appName = request.getParameter("appName");
System.out.println("第三方应用多图片上传uploadify使用的UploadController中测试appNo:"+appNo);
while (it.hasNext()) {
FileItem item = it.next();
if (item.isFormField()) {
if ("category".equals(item.getFieldName())) {
category = item.getString("utf-8");
}
} else if (!item.isFormField()) {
name = item.getName();
System.out.println("第三方应用多图片上传uploadify使用的UploadController中测试name:"+name);
if (name == null || name.trim().equals("")) {
continue;
}
// 扩展名格式:
if (name.lastIndexOf(".") >= 0) {
extName = name.substring(name.lastIndexOf("."));
}
savePath = savePath + category + "/";
File f1 = new File(savePath);
if (!f1.exists()) {
f1.mkdirs();
}
File saveFile = new File(savePath + name);
try {
item.write(saveFile);
} catch (Exception e) {
e.printStackTrace();
}
}
}
response.getWriter().print(name);
}
}
<script type="text/javascript">
$(document).ready(function() {
$("#uploadify").uploadify({
'uploader' : '<%=request.getContextPath()%>/uploadify.swf',
'script' : '<%=basePath %>/UploadController',
'cancelImg' : '<%=request.getContextPath()%>/images/cancel.png',
'queueID' : 'fileQueue',
'auto' : false,
'multi' : true,
'buttonText' : 'Browse file',
'fileExt': '*.jpg;*.gif;*.jpeg;*.png;*.bmp',
'fileDesc': '请选择jpg,gif,jpeg,png,bmp格式',
'queueSizeLimit' : 5,
onComplete: function (evt, queueID, fileObj, response, data) {
$('#appImg').val($('#appImg').val()+response+',');
},
onError: function(a, b, c, d) {
alert("文件上传失败");
}
});
});
</script>
后台servlet
public class UploadController extends HttpServlet {
private static final long serialVersionUID = 1L;
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setCharacterEncoding("utf-8");
String savePath = request.getSession().getServletContext().getRealPath(File.separator+"AppImgPackage"+File.separator);
DiskFileItemFactory fac = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(fac);
upload.setHeaderEncoding("utf-8");
List fileList = null;
try {
fileList = upload.parseRequest(request);
} catch (FileUploadException ex) {
ex.printStackTrace();
}
Iterator<FileItem> it = fileList.iterator();
String name = "";
String extName = "";
String category = "";
String appNo = request.getParameter("appNo");
String appName = request.getParameter("appName");
System.out.println("第三方应用多图片上传uploadify使用的UploadController中测试appNo:"+appNo);
while (it.hasNext()) {
FileItem item = it.next();
if (item.isFormField()) {
if ("category".equals(item.getFieldName())) {
category = item.getString("utf-8");
}
} else if (!item.isFormField()) {
name = item.getName();
System.out.println("第三方应用多图片上传uploadify使用的UploadController中测试name:"+name);
if (name == null || name.trim().equals("")) {
continue;
}
// 扩展名格式:
if (name.lastIndexOf(".") >= 0) {
extName = name.substring(name.lastIndexOf("."));
}
savePath = savePath + category + "/";
File f1 = new File(savePath);
if (!f1.exists()) {
f1.mkdirs();
}
File saveFile = new File(savePath + name);
try {
item.write(saveFile);
} catch (Exception e) {
e.printStackTrace();
}
}
}
response.getWriter().print(name);
}
}
#1
页面
<script type="text/javascript">
$(document).ready(function() {
$("#uploadify").uploadify({
'uploader' : '<%=request.getContextPath()%>/uploadify.swf',
'script' : '<%=basePath %>/UploadController',
'cancelImg' : '<%=request.getContextPath()%>/images/cancel.png',
'queueID' : 'fileQueue',
'auto' : false,
'multi' : true,
'buttonText' : 'Browse file',
'fileExt': '*.jpg;*.gif;*.jpeg;*.png;*.bmp',
'fileDesc': '请选择jpg,gif,jpeg,png,bmp格式',
'queueSizeLimit' : 5,
onComplete: function (evt, queueID, fileObj, response, data) {
$('#appImg').val($('#appImg').val()+response+',');
},
onError: function(a, b, c, d) {
alert("文件上传失败");
}
});
});
</script>
后台servlet
public class UploadController extends HttpServlet {
private static final long serialVersionUID = 1L;
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setCharacterEncoding("utf-8");
String savePath = request.getSession().getServletContext().getRealPath(File.separator+"AppImgPackage"+File.separator);
DiskFileItemFactory fac = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(fac);
upload.setHeaderEncoding("utf-8");
List fileList = null;
try {
fileList = upload.parseRequest(request);
} catch (FileUploadException ex) {
ex.printStackTrace();
}
Iterator<FileItem> it = fileList.iterator();
String name = "";
String extName = "";
String category = "";
String appNo = request.getParameter("appNo");
String appName = request.getParameter("appName");
System.out.println("第三方应用多图片上传uploadify使用的UploadController中测试appNo:"+appNo);
while (it.hasNext()) {
FileItem item = it.next();
if (item.isFormField()) {
if ("category".equals(item.getFieldName())) {
category = item.getString("utf-8");
}
} else if (!item.isFormField()) {
name = item.getName();
System.out.println("第三方应用多图片上传uploadify使用的UploadController中测试name:"+name);
if (name == null || name.trim().equals("")) {
continue;
}
// 扩展名格式:
if (name.lastIndexOf(".") >= 0) {
extName = name.substring(name.lastIndexOf("."));
}
savePath = savePath + category + "/";
File f1 = new File(savePath);
if (!f1.exists()) {
f1.mkdirs();
}
File saveFile = new File(savePath + name);
try {
item.write(saveFile);
} catch (Exception e) {
e.printStackTrace();
}
}
}
response.getWriter().print(name);
}
}
<script type="text/javascript">
$(document).ready(function() {
$("#uploadify").uploadify({
'uploader' : '<%=request.getContextPath()%>/uploadify.swf',
'script' : '<%=basePath %>/UploadController',
'cancelImg' : '<%=request.getContextPath()%>/images/cancel.png',
'queueID' : 'fileQueue',
'auto' : false,
'multi' : true,
'buttonText' : 'Browse file',
'fileExt': '*.jpg;*.gif;*.jpeg;*.png;*.bmp',
'fileDesc': '请选择jpg,gif,jpeg,png,bmp格式',
'queueSizeLimit' : 5,
onComplete: function (evt, queueID, fileObj, response, data) {
$('#appImg').val($('#appImg').val()+response+',');
},
onError: function(a, b, c, d) {
alert("文件上传失败");
}
});
});
</script>
后台servlet
public class UploadController extends HttpServlet {
private static final long serialVersionUID = 1L;
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setCharacterEncoding("utf-8");
String savePath = request.getSession().getServletContext().getRealPath(File.separator+"AppImgPackage"+File.separator);
DiskFileItemFactory fac = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(fac);
upload.setHeaderEncoding("utf-8");
List fileList = null;
try {
fileList = upload.parseRequest(request);
} catch (FileUploadException ex) {
ex.printStackTrace();
}
Iterator<FileItem> it = fileList.iterator();
String name = "";
String extName = "";
String category = "";
String appNo = request.getParameter("appNo");
String appName = request.getParameter("appName");
System.out.println("第三方应用多图片上传uploadify使用的UploadController中测试appNo:"+appNo);
while (it.hasNext()) {
FileItem item = it.next();
if (item.isFormField()) {
if ("category".equals(item.getFieldName())) {
category = item.getString("utf-8");
}
} else if (!item.isFormField()) {
name = item.getName();
System.out.println("第三方应用多图片上传uploadify使用的UploadController中测试name:"+name);
if (name == null || name.trim().equals("")) {
continue;
}
// 扩展名格式:
if (name.lastIndexOf(".") >= 0) {
extName = name.substring(name.lastIndexOf("."));
}
savePath = savePath + category + "/";
File f1 = new File(savePath);
if (!f1.exists()) {
f1.mkdirs();
}
File saveFile = new File(savePath + name);
try {
item.write(saveFile);
} catch (Exception e) {
e.printStackTrace();
}
}
}
response.getWriter().print(name);
}
}