文件上传重命名
我是用uploadify做的多文件上传,
如图,点击“开始上传”,文件上传到服务器(页面下面有一个提交按钮的,我没有截图出来,点击“开始上传”时还没有提交整个页面),如何将要上传的文件(文件个数不定)进行重命名,图上原名为grass.jpg和look.jpg,修改成以 软件编号+序号的方式,即2013122616231.jpg和2013122616232.jpg存储到服务器呢?
上传页面软件标号name定为appNo,在uploadify后台处理的servlet中,我使用
String appNo = request.getParameter("appNo");获取不到appNo。只要获取到了appNo就好弄了请教如何解决。
[解决办法]
我以前写了一个 给你自己看吧 ,我这个当时乱码 ,转换了一下 。
public ActionForward insert(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
UserInfo po =new UserInfo();
DiskFileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(factory);
// 最大允许大小5M
//upload.setSizeMax(1024 * 1024 * 5);
String appName=request.getContextPath();
String path="../webapps"+appName+"/files/";
String purl="";
try {
List<FileItem> items = upload.parseRequest((HttpServletRequest) request);
for (FileItem item : items) {
if (!item.isFormField()) {
String fname = item.getName();
path=path+fname;
purl="files/"+fname;
//System.out.println(path+"$$$"+purl);
File f=new File(path);
if(f.getParentFile().exists()) f.getParentFile().mkdirs();
f.createNewFile();
FileOutputStream fi=new FileOutputStream(f);
InputStream in=item.getInputStream();
byte buffer[] = new byte[8192];
int bytesRead=0;
while ( (bytesRead = in.read(buffer, 0, 8192)) != -1){
fi.write(buffer, 0, bytesRead);
}
in.close();
fi.close();
}else {
if("name".equals(item.getFieldName())){
String name = new String(item.getString()
.getBytes("ISO-8859-1"),"UTF-8");
po.setName(name );
}
if("city".equals(item.getFieldName()))
{
String city = new String(item.getString()
.getBytes("ISO-8859-1"),"UTF-8");
po.setCity(city);
}
if("number".equals(item.getFieldName())) {
String s=item.getString();
po.setNumber(s);
po.setBirthday(subStr(s));
};
if("province".equals(item.getFieldName())) {
String province = new String(item.getString()
.getBytes("ISO-8859-1"),"UTF-8");
po.setProvince(province);
}
if("sex".equals(item.getFieldName()))
{
String sex = new String(item.getString()
.getBytes("ISO-8859-1"),"UTF-8");
po.setSex(sex);
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
po.setPurl(purl);
service.insert(po);
return mapping.findForward("susses");
}
[解决办法]
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 = "";
while (it.hasNext()) {
FileItem item = it.next();
String appNo = item.getString("appNo");
System.out.println("第三方应用多图片上传uploadify使用的UploadController中测试appNo:"+appNo);
if (item.isFormField()) {
String filedName = item.getFieldName();
if (filedName.equals("appNo")) {
String appNo= item.getString();
System.out.print(appNo);
}
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);