本来我的图片上传是没有问题的;
那是因为我上传的图片名称都是英文或是数字的;
从来,没有长传过中文名称的图片;
上传之后,就图片名称就变成乱码;
我的图片上传是在struts里面完成的;
请问怎么办?下面是我的action里面的一段代码:
protected void executeNew(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) {
NewsForm newsForm=(NewsForm)form;
FormFile file=newsForm.getFile();
System.out.println("------file:"+file);
if(file==null){
newsForm.setForward(newsForm.FORWARD_EDIT_ADD);
}
String filename = file.getFileName();
System.out.println("------filename:"+filename);
newsForm.setFileName(filename);
String size = Integer.toString(file.getFileSize()) + "bytes";
newsForm.setSize(size);
InputStream is = null;
try {
is = file.getInputStream();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
String store_path = servlet.getServletContext().getRealPath("/newsPicUpload");
System.out.println("------store_path:"+store_path);
String temp="/newsPicUpload" + "/" + filename;
OutputStream os = null;
try {
os = new FileOutputStream(store_path + "/" + filename);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
System.out.print("temp:"+temp); int bytes = 0;
byte [] buffer = new byte[8192];
try {
while ((bytes = is.read(buffer,0,8192))!=-1){
os.write(buffer,0,bytes);
System.out.println("bytes:"+bytes);
}
} catch (IOException e) {
e.printStackTrace();
}
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
file.destroy();
this.newsService.save(null, newsForm.getNewTitle(), newsForm.getNewContent(),
newsForm.getNewDate(), this.newsPicService.save(temp));
request.setAttribute("newTitle",newsForm.getNewTitle());
request.setAttribute("newContent",newsForm.getNewContent());
request.setAttribute("newDate", newsForm.getNewDate());
request.setAttribute("newUrl",temp);
List list=this.newsService.findByLastRecord();
request.setAttribute("list", list);
}
那是因为我上传的图片名称都是英文或是数字的;
从来,没有长传过中文名称的图片;
上传之后,就图片名称就变成乱码;
我的图片上传是在struts里面完成的;
请问怎么办?下面是我的action里面的一段代码:
protected void executeNew(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) {
NewsForm newsForm=(NewsForm)form;
FormFile file=newsForm.getFile();
System.out.println("------file:"+file);
if(file==null){
newsForm.setForward(newsForm.FORWARD_EDIT_ADD);
}
String filename = file.getFileName();
System.out.println("------filename:"+filename);
newsForm.setFileName(filename);
String size = Integer.toString(file.getFileSize()) + "bytes";
newsForm.setSize(size);
InputStream is = null;
try {
is = file.getInputStream();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
String store_path = servlet.getServletContext().getRealPath("/newsPicUpload");
System.out.println("------store_path:"+store_path);
String temp="/newsPicUpload" + "/" + filename;
OutputStream os = null;
try {
os = new FileOutputStream(store_path + "/" + filename);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
System.out.print("temp:"+temp); int bytes = 0;
byte [] buffer = new byte[8192];
try {
while ((bytes = is.read(buffer,0,8192))!=-1){
os.write(buffer,0,bytes);
System.out.println("bytes:"+bytes);
}
} catch (IOException e) {
e.printStackTrace();
}
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
file.destroy();
this.newsService.save(null, newsForm.getNewTitle(), newsForm.getNewContent(),
newsForm.getNewDate(), this.newsPicService.save(temp));
request.setAttribute("newTitle",newsForm.getNewTitle());
request.setAttribute("newContent",newsForm.getNewContent());
request.setAttribute("newDate", newsForm.getNewDate());
request.setAttribute("newUrl",temp);
List list=this.newsService.findByLastRecord();
request.setAttribute("list", list);
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货