上传并保存到文件夹时,如何使用java更改图像的名称?
}上传并保存到文件夹时,如何使用java更改图像的名称?,java,jsp,servlets,file-rename,Java,Jsp,Servlets,File Rename,} 有谁能指导我如何动态地更改它。提前谢谢。我不知道Servlet和类似的东西是如何工作的,但是我可以给你一个你需要做的简要说明 在DemoServlet中,需要输入Image_Name字段,并将其作为FileUpload的参数之一 public class FileUpload { public static boolean processFile(String path, FileItemStream item) { try { File f = new File(
有谁能指导我如何动态地更改它。提前谢谢。我不知道Servlet和类似的东西是如何工作的,但是我可以给你一个你需要做的简要说明 在DemoServlet中,需要输入Image_Name字段,并将其作为FileUpload的参数之一
public class FileUpload {
public static boolean processFile(String path, FileItemStream item) {
try {
File f = new File(path + File.separator + "web/images");
if (!f.exists()) {
f.mkdir();
}
File savedFile = new File(f.getAbsolutePath() + File.separator + item.getName());
FileOutputStream fos = new FileOutputStream(savedFile);
InputStream is = item.openStream();
int x = 0;
byte[] b = new byte[1024];
while ((x = is.read(b)) != -1) {
fos.write(b, 0, x);
}
fos.flush();
fos.close();
return true;
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
因为当前processFile方法从FileItemStream中获取文件名。您需要将其从该文件名更改为实际文件名
public static boolean processFile(String path, FileItemStream item, String fileName){
//Method Code
}
到
在上面的代码中,您可以随时更改文件名,它将自动使用此名称保存。您可以在java类代码中更改图像的名称
List fileItems = upload.parseRequest(request);
Iterator i = fileItems.iterator();
System.out.println("In >>>>>>>>>>>>>>> :: "+fileItems);
while(i.hasNext()){
FileItem fi = (FileItem) i.next();
System.out.println("Val <<<<>>>>>>:: "+fi);
if(fi.isFormField()){
String fieldName = fi.getFieldName();
String val = fi.getString();
System.out.println(fieldName+" :: Val :: "+val);
}else{
String fileName = fi.getName();
String root = getServletContext().getRealPath("/");
File path = new File(root+"/uploads");
if (!path.exists()) {
boolean status = path.mkdir();
}
File uploadFile = new File(path+"/"+fileName);
fi.write(uploadFile);
}
}
您必须在方法中传递文件名。
您可以指定您的名称,而不是item.getName 假设我第二次上传图像,图像的实际名称为FirstTemple.jpg,我将其命名为Temple,它是否将另存为Temple.jpg?请更改我的代码并将其还原为FileUpload.processFilepath,item,这里我应该传递什么参数每次我们无法在代码中给出名称时,我们没有在文本字段中给出名称,该名称必须为指定的图像设置是的,这也很好,看到一个问题有很多解决方案,我只是建议了一个,你能更改我的代码吗?我已经更改了你的代码…而不是item.getName。你只需要传递图像的名称…根据问题增强你的答案,因为它不是当前的答案。这不回答问题。如果你有自己的问题,请发布一个新的问题,如果相关的话,你应该包括一个到这个问题的链接。
public class FileUpload {
public static boolean processFile(String path, FileItemStream item , String name) {
try {
File f = new File(path + File.separator + "web/images");
if (!f.exists()) {
f.mkdir();
}
File savedFile = new File(f.getAbsolutePath() + File.separator + item.getName()); // instead of item.getName() you can give your name.
FileOutputStream fos = new FileOutputStream(savedFile);
InputStream is = item.openStream();
int x = 0;
byte[] b = new byte[1024];
while ((x = is.read(b)) != -1) {
fos.write(b, 0, x);
}
fos.flush();
fos.close();
return true;
} catch (Exception e) {
e.printStackTrace();
}
return false;
File savedFile = new File(f.getAbsolutePath() + File.separator + item.getName());
File savedFile = new File(f.getAbsolutePath() + File.separator + fileName + ".png");
List fileItems = upload.parseRequest(request);
Iterator i = fileItems.iterator();
System.out.println("In >>>>>>>>>>>>>>> :: "+fileItems);
while(i.hasNext()){
FileItem fi = (FileItem) i.next();
System.out.println("Val <<<<>>>>>>:: "+fi);
if(fi.isFormField()){
String fieldName = fi.getFieldName();
String val = fi.getString();
System.out.println(fieldName+" :: Val :: "+val);
}else{
String fileName = fi.getName();
String root = getServletContext().getRealPath("/");
File path = new File(root+"/uploads");
if (!path.exists()) {
boolean status = path.mkdir();
}
File uploadFile = new File(path+"/"+fileName);
fi.write(uploadFile);
}
public class FileUpload {
public static boolean processFile(String path, FileItemStream item , String name) {
try {
File f = new File(path + File.separator + "web/images");
if (!f.exists()) {
f.mkdir();
}
File savedFile = new File(f.getAbsolutePath() + File.separator + item.getName()); // instead of item.getName() you can give your name.
FileOutputStream fos = new FileOutputStream(savedFile);
InputStream is = item.openStream();
int x = 0;
byte[] b = new byte[1024];
while ((x = is.read(b)) != -1) {
fos.write(b, 0, x);
}
fos.flush();
fos.close();
return true;
} catch (Exception e) {
e.printStackTrace();
}
return false;
//How does not work in this way?Please tell me another way.
import java.io.File;
public class RenameFileExample {
public static void main(String[] args)
{
File oldfile =new File("oldfile.txt");
File newfile =new File("newfile.txt");
File file = new File("oldfilename.png");
file.renameTo(new File("newfilename.png"));
System.out.println("Rename To:"+file.getName());
if(oldfile.renameTo(newfile)){
System.out.println("Rename succesful");
}else{
System.out.println("Rename failed");
}
}
}