gpt4 book ai didi

java - 使用 Struts 上传图片和保存 Url

转载 作者:行者123 更新时间:2023-11-30 09:48:27 25 4
gpt4 key购买 nike

我有一些代码可以使用 struts2 将图像上传到服务器。上传后我想将我的图像显示为缩略图并发送我上传到数据库的图像路径。我很困惑它是如何工作的

这是我的操作代码。Cimande 操作是从 ActionSupport 扩展而来的。

package com.maetrika.jagatoko.controller;

import java.io.File;
import java.io.FilePermission;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.io.FileUtils;
import org.blueoxygen.cimande.commons.CimandeAction;

public class FileUpload extends CimandeAction{

private File upload;
private String uploadContentType;
private String uploadFileName;
private String fileCaption;

private HttpServletRequest servletRequest;

File file;

public String execute() throws Exception{

try{

//String filePath=servletRequest.getRealPath("/");
String filePath=servletRequest.getSession().getServletContext().getRealPath("/");
File fileToCreate=new File(filePath, this.uploadFileName);
FileUtils.copyFile(upload, fileToCreate);
}catch (Exception e) {
e.printStackTrace();
return INPUT;
// TODO: handle exception
}



return SUCCESS;
}






public File getUpload() {

return upload;
}

public void setUpload(File upload) {
this.upload = upload;
}

public String getUploadContentType() {
return uploadContentType;
}

public void setUploadContentType(String uploadContentType) {
this.uploadContentType = uploadContentType;
}

public String getUploadFileName() {

System.out.println("=======================" + uploadFileName);
return uploadFileName;

}

public void setUploadFileName(String uploadFileName) {
this.uploadFileName = uploadFileName;
}

public String getFileCaption() {
return fileCaption;
}

public void setFileCaption(String fileCaption) {
this.fileCaption = fileCaption;
}



public HttpServletRequest getServletRequest() {
return servletRequest;
}



public void setServletRequest(HttpServletRequest servletRequest) {
this.servletRequest = servletRequest;
}



public File getFile() {
return file;
}



public void setFile(File file) {
this.file = file;
}


}

我使用速度来查看。这是我上传图片的代码

<html>
<head>
</head>

<body>
<form method="post" action="doUpload" enctype="multipart/form-data">
<table border="0" cellpadding="2">
<tr>
<td>Upload Gambar Produk</td>
</tr>

<tr>
<td><input type="file" name="upload"></td>
</tr>

<tr>
<td><input type="submit" value="submit"/>
</tr>
</table>

</form>
</body>
</html>

我是 Java 编程的新手。请帮助我..

最佳答案

您只需将图像名称存储在数据库中,以毫秒为单位生成时间戳以避免重复

    String fileName = myFile.getFileName();
String extension = "";

if (fileName != null && !fileName.equals("")) {
extension = fileName.substring(fileName.lastIndexOf("."), fileName.length());
}

long longName = Calendar.getInstance().getTimeInMillis(); //date in miliseconds
String newFileName = longName + extension;

现在您想将图像存储在您的项目中以备将来使用。为此,在您的根文件夹中创建一个名为 userimages 的文件夹,您的 Action 代码将如下所示:

String uploadFolder = "userimages";
String filePath = getServlet().getServletContext().getRealPath("/") + uploadFolder;

/* Save file on the server */
if (!fileName.equals("")) {
System.out.println("Server path:" + filePath);

//Create file
File fileToCreate = new File(filePath, newFileName);

//If file does not exists, create file
if (!fileToCreate.exists()) {
FileOutputStream fileOutStream = new FileOutputStream(fileToCreate);
fileOutStream.write(myFile.getFileData());
fileOutStream.flush();
fileOutStream.close();
}

}

现在,要在您的 jsp 中显示图像,您需要使用 EL

类似的东西:

 <img src="${pageContext.request.contextPath}/userimages/${imagename}"/>

希望对你有帮助

关于java - 使用 Struts 上传图片和保存 Url,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6237291/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com