我已經創建了動態webproject,我使用下面的代碼來上傳圖像並從相同的位置檢索。當我試圖在外部瀏覽器中運行該應用程序時,它顯示矩形框而不是image.getting在外部Web瀏覽器中加載出錯。圖像無法加載在外部web瀏覽器(Firefox)
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.Iterator;
import java.util.List;
import java.util.Random;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.sql.*;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.commons.fileupload.disk.*;
import org.apache.commons.fileupload.*;
public class UploadImage extends HttpServlet {
private static final long serialVersionUID = 1L;
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
PrintWriter out = response.getWriter();
boolean isMultipart = ServletFileUpload.isMultipartContent(request);
System.out.println("request: " + request);
if (!isMultipart) {
System.out.println("File Not Uploaded");
} else {
FileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(factory);
List items = null;
try
{
items = upload.parseRequest(request);
System.out.println("items: " + items);
}
catch (FileUploadException e)
{
e.printStackTrace();
}
Iterator itr = items.iterator();
while (itr.hasNext()) {
FileItem item = (FileItem) itr.next();
if (item.isFormField())
{
String name = item.getFieldName();
System.out.println("name: " + name);
String value = item.getString();
System.out.println("value: " + value);
}
else
{
try
{
String itemName = item.getName();
Random generator = new Random();
int r = Math.abs(generator.nextInt());
String reg = "[.*]";
String replacingtext = "";
System.out.println("Text before replacing is:-"+ itemName);
Pattern pattern = Pattern.compile(reg);
Matcher matcher = pattern.matcher(itemName);
StringBuffer buffer = new StringBuffer();
while (matcher.find())
{
matcher.appendReplacement(buffer, replacingtext);
}
int IndexOf = itemName.indexOf(".");
String domainName = itemName.substring(IndexOf);
System.out.println("domainName: " + domainName);
String finalimage = buffer.toString() + "_" + r+ domainName;
System.out.println("Final Image===" + finalimage);
File savedFile = new File("D:\\test1\\" + "images\\"+ finalimage);
item.write(savedFile);
out.println("<html>");
out.println("<body>");
out.println("<table><tr><td>");
out.println("<img src=D:\\test1\\images\\" + finalimage+ ">");
out.println("</td></tr></table>");
Connection conn = null;
String url = "jdbc:oracle:thin:@//localhost:1521/erp";
String username = "system";
String userPassword = "manager";
String strQuery = null;
try
{
System.out.println("itemName::::: " + itemName);
Class.forName("oracle.jdbc.driver.OracleDriver");
conn = DriverManager.getConnection(url, username,userPassword);
Statement st = conn.createStatement();
strQuery = "insert into testimage values('"+ finalimage + "')";
int rs = st.executeUpdate(strQuery);
System.out.println("Query Executed Successfully++++++++++++++");
out.println("image inserted successfully");
out.println("</body>");
out.println("</html>");
}
catch (Exception e)
{
System.out.println(e.getMessage());
}
finally
{
conn.close();
}
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
}
}
}
您正在設置引用本地文件的'src'屬性:'D:\\ ...'。當然,一個「外部」客戶對此一無所知。 – ubik 2013-04-20 10:16:17
那麼如何給D盤提供一個路徑,讓瀏覽器能夠加載圖片? – user2169941 2013-04-20 10:24:24
這就是問題所在。你應該**不要**使用本地路徑。您應該將圖像保存在服務器的某個位置,並在其中提供GET端點,以便在請求時返回圖像。 'src'屬性應該是該端點的URL。 – ubik 2013-04-20 10:26:20